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中的数据类型检测的更多相关文章

  1. js中判断数据类型的四种方法总结

    js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...

  2. js中的数据类型

    JS中的数据类型: ——数字  (number)NaN ——字符串(string) ——布尔  (boolean)——函数  (function)     也是对象的一种 ——对象  (object) ...

  3. 如何判断js中的数据类型?

    js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...

  4. 如何判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  5. [转]如何判断js中的数据类型

    原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructo ...

  6. 浅谈js中的数据类型,使用typeof获取js数据类型

    JS中的数据类型 1):Undefined——值未定义 注:Undefined类型只有一个值,即特色的undefined.在使用var声明变量但未对其加以初始化时,这个变量的值就是undefined ...

  7. 如何判断js中的数据类型(转)

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  8. 判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  9. js中的数据类型及其转换

    Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...

随机推荐

  1. 集群节点间网络通信TIPC

    1. TIPC背景介绍 TIPC主要是用于集群网络环境之中,它这个协议有一些前提假设包括: 协议发送的大部分message都是直接到达目的地(无路由): message的传输时间都很短; messag ...

  2. 软件工程(C编码实践篇)总结

    陆伟丹 + 原创作品转载请注明出处 + <软件工程(C编码实践篇)>MOOC课程http://mooc.study.163.com/course/USTC-1000002006 对软件工程 ...

  3. -/bin/sh: ./led: not found的解决办法

    环境介绍: 开发板:qq2440 交叉编译器:arm-linux-gcc 3.4.1 内核版本:2.6.13 一.针对该类问题从两个方面入手: 1.从权限出发,权限不够会出现此问题 2.从库文件出发, ...

  4. Xcode7 *** does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE)

    *** does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE ...

  5. 浅析word-break work-wrap区别

    word-break:[断词] 定义:规定自动换行的处理方法.   注:通过word-break使用,可以实现让浏览器在任意位置换行. 语法:word-break: normal|break-all| ...

  6. treap 模版

    struct Treap { struct node { node *son[]; int key,siz,wei,cnt; node(int _key,node *f) { son[]=son[]= ...

  7. C# java MD5加密方不一致问题

    说来惭愧,做开发几年了,一直在吸取,今天也写写自已关于技术的一点点理解,不正之处,请大家多多指点. 由于之前开发的项目使用的是C#,用户信息使用的C#的MD5加密码方式,而现在需要切换到Java平台下 ...

  8. CSS3盒子阴影box-shadow

    来自W3CSchool的解释 语法: box-shadow: h-shadow v-shadow blur spread color inset; 注释:box-shadow 向框添加一个或多个阴影. ...

  9. linux下添加环境变量

    我安装完 RedHat Linux 5 之后,在终端使用一些命令,如: ifcinfig 查看本机的IP,发现不能使用此命令,提示说“command not found”,这该怎么办呢 想想肯定是环境 ...

  10. Makefile中头文件在依赖关系中作用

    摘于:http://bbs.csdn.net/topics/120024677 (1)在makefile的依赖关系中用不用体现.h头文件?(2)如果在依赖关系中要体现.h头文件,应该体现到什么层次?= ...