JS中的数据类型检测
JavaScript的数据类型分为两类:原始类型(primitive type)和对象类型(object type)。原始类型有5种,分别是:数字(Number)、字符串(String)、布尔值(Boolean)、null和undefined。引用类 型包括:Object、Array、Date、Error、RegExp、Functions。
1.原始类型的检测
对于原始类型中的:数字、字符串、布尔值、undefined,最佳检测方法是使用typeof运算符,typeof检测后会返回一个表示值类型的字符串。typeof的语法是:
typeof <变量>
typeof(<变量>)
对以上4种原始值使用typeof运算符后:数字返回"number" 、 字符串返回"string" 、 布尔值返回"boolean" 、 undefined返回"undefined"。如下:
var str = 'a string';
console.log(typeof str); // -> string var num = 0;
console.log(typeof num); // -> string var bool = true;
console.log(typeof bool); // -> boolean var undefin;
console.log(typeof undefin); // -> undefined 未定义的变量和未初始化的变量都将返回undefined
原始值null,应当使用===和!==进行行比较,对null做typeof检测时,返回值为"object"。
2.引用类型的检测
引用值也称做对象(object),在JavaScript中除原始类型外都是引用类型。typeof运算符不能有效检测引用类型,所有引用类型都会返回“object”:
console.log(typeof []); // -> object
console.log(typeof new Date()); // -> object
console.log(typeof {}); // -> object
console.log(typeof new Error()); // -> object
console.log(typeof new RegExp()); // -> object
console.log(typeof Function); // -> function
引用类型检测推荐使用instanceof运算符,基本语法是:
value instanceof constructor
对引用类型做instanceof操作:
console.log([] instanceof Array); // -> true
console.log(new Date() instanceof Date); // -> true
console.log({} instanceof Object); // -> true
console.log(new Error() instanceof Error); // -> true
var reg = new RegExp();
console.log(reg instanceof RegExp); // -> true
function func (argument) {
}
console.log(func instanceof Function) // -> true
对于函数做typeof操作返回function,相比intanceof更推荐使用typeof。
对象属性检测
判断属性是否存在最好的方法是使用in运算符,如果属性存在或继承自原型链,都会返回true。如果只想检测对象实例是否存在某属性,而不是从原型继承的属性则使用hasOwnProperty()方法。如下:
function Person() {
this.name = 'liuht';
this.doSometing = function() {
return 'doSometing';
}
}
Person.prototype.area = 'China'; // 原型链属性
var man = new Person();
man.sex = 'man';
console.log('name' in man); // -> true
console.log('sex' in man); // -> true
console.log('area' in man); // -> true
console.log(man.hasOwnProperty('name')); // -> true
console.log(man.hasOwnProperty('sex')); // -> true
console.log(man.hasOwnProperty('area')); // -> false
JS中的数据类型检测的更多相关文章
- js中判断数据类型的四种方法总结
js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...
- js中的数据类型
JS中的数据类型: ——数字 (number)NaN ——字符串(string) ——布尔 (boolean)——函数 (function) 也是对象的一种 ——对象 (object) ...
- 如何判断js中的数据类型?
js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...
- 如何判断js中的数据类型
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- [转]如何判断js中的数据类型
原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructo ...
- 浅谈js中的数据类型,使用typeof获取js数据类型
JS中的数据类型 1):Undefined——值未定义 注:Undefined类型只有一个值,即特色的undefined.在使用var声明变量但未对其加以初始化时,这个变量的值就是undefined ...
- 如何判断js中的数据类型(转)
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- 判断js中的数据类型
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- js中的数据类型及其转换
Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...
随机推荐
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
委托和事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人每次见 ...
- QLPreViewController的初步实用
前一阵项目需要添加一个文档文件的查看功能,于是就各种找资料,一开始想实用webView,然而webView有的格式不支持,而且占内存太大了.找着找着就找到QLPreViewController.用了一 ...
- crontab Job权限重要
在撰写JOb时,需要将SH目录及文件设为777,方可正常执行~
- Ubuntu 16.04安装搜狗输入法
转载: http://www.it610.com/article/5319575.htm 打开firefox浏览器,输入网址www.baidu.com,打开后搜索搜狗拼音 linux进入到搜狗拼音li ...
- css Animation初体验
目前有越来越多的网站都使用animation,无论他们是用GIF,SVG,WebGL,视频背景或者其他形式展示.适当地使用动画会让网站更生动,互动性更好,为用户增加一个额外的反馈和体验层. 在本教程中 ...
- css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?一起来$('.float')
一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class="outer"> <div class=&quo ...
- TCP/IP协议学习(五) 基于C# Socket的C/S模型
TCP/IP协议作为现代网络通讯的基石,内容包罗万象,直接去理解理论是比较困难的:然而通过实践先理解网络通讯的理解,在反过来理解学习TCP/IP协议栈就相对简单很多.C#通过提供的Socket API ...
- CSRF token 无法被验证. ----Yii连接数据库后数据库错误日志报错
CSRF token 无法被验证. 我使用的是mongodb+ yii1.1 What is CSRF, please see the details here. http://en.wikiped ...
- QQ号验证
№☆★〇█962319751,1611385654,209414856,2729767026,1213235712,1187970122,1393756744,2463317013,346171055 ...
- 在重新生成解决方案时,出现的错误:无法将文件“obj\x86\Debug\*.exe”复制到“obj\Debug\*.exe”。文件正由另一进程使用,因此该进程无法访问此文件
此例是VS2010的CS项目. 在重新生成解决方案时,出现的错误. 解决步骤:先关闭解决方案,再在项目文件下的bin\Debug\*.exe删除这类之前生成得.exe文件,再在VS2010下重新生成.