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数据类型指的是数字,可以为整型, ...
随机推荐
- background-sizi (转)
http://www.cnblogs.com/greenteaone/archive/2012/08/28/2659878.html (原创作者链接地址 ) Background-Size:[ & ...
- PL/SQL通过免安装客户端连接远端ORACLE数据库
参考百度经验:http://jingyan.baidu.com/article/375c8e19b4094d25f2a2291a.html
- Signatures of Mutational Processes in Human Cancer
http://cancer.sanger.ac.uk/cosmic/signatures#
- ZigBee 安全探究
ZigBee 安全探究 0x02 ZigBee安全机制 (注:对于本节内容,可能在新版ZigBee协议标准中会有所变化,请以新版为准.) ZigBee主要提供有三个等级的安全模式: 1. 非安全模式: ...
- Struts2 简介
回顾Struts2的使用过程,网上搜的教程多多少少都会有点问题,重新记录下创建过程,方便查阅. 1.下载Struts2的jar包 下载地址:http://archive.apache.org/dist ...
- 远程重装centos6
写得比较简略,也是综合网络上的文章,总结一下实操的经验 获取启动内核 wget -P /boot/ http://mirrors.163.com/centos/6.8/os/x86_64/images ...
- 【High-Speed and Accurate Laser Scan Matching Using Classified Features】
所谓的"分类特征",就是把特征分成 1. 旋转特征:用直线表示 2. 平移特征,用撕裂点和临界点表示 最大的创新点 应该就是下面的分组吧 匹配的时候,用RANSAC 或者动态规划, ...
- hdu acm 简单暴力1004
字符串匹配函数strcmp 直接使用来判断两字符串是否完全相等 用数组存每个单词的个数时 初始化为零就错 初始化为一时就正确 也不知道为什么
- aaa
aaaa 来自为知笔记(Wiz)
- LOL
当输入数据一样时,计算结果是一样的,但运行时间的差别很大.在算法正确的前提下,应该选择算法效率高的 嵌入式软件,BSP驱动,通信协议,上层应用软件. 多协议标签交换(MPLS)是一种用于快速数据包交换 ...