判断JS的数据类型
typeof、instanceof、 constructor、 prototype方法比较
(摘自如何判断JS中的数据类型)
1. 使用typeof操作符。
对一个值使用 typeof 操作符可能返回下列某个字符串,返回的类型都是字符串形式。
(1) undefined:如果这个值未定义
(2) boolean:如果这个值是布尔值
(3) string:如果这个值是字符串
(4) number:如果这个值是数值
(5) object:如果这个值是对象或null
(6) function:如果这个值是函数
需要注意:typeof不适合用于判断是否为数组。当使用typeof判断数组和对象的时候,都会返回object。
可以使用isArray()来判断是否为数组。
2. instanceof
instanceof 运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上。需要区分大小写。
简单的来说,instanceof 用于判断一个变量是否某个对象的实例。
例:var arr = new Array( );
alert(arr instanceof Array); // 返回true
需要注意的是,instanceof只能用来判断对象和函数,不能用来判断字符串和数字等。判断它是否为字符串和数字时,只会返回false。
instanceof 是用来判断 A 是否为 B 的实例,表达式为:A instanceof B,如果 A 是 B 的实例,则返回 true,否则返回 false。 在这里需要特别注意的是:instanceof 检测的是原型,我们用一段伪代码来模拟其内部执行过程:
|
1
2
3
4
5
6
7
8
9
|
instanceof (A,B) = { var L = A.__proto__; var R = B.prototype; if(L === R) { // A的内部属性 __proto__ 指向 B 的原型对象 return true; } return false;} |
从上述过程可以看出,当 A 的 __proto__ 指向 B 的 prototype 时,就认为 A 就是 B 的实例,我们再来看几个例子:
|
1
2
3
4
5
6
7
8
9
10
|
[] instanceof Array;// true{} instanceof Object;// truenew Date() instanceof Date;// truefunction Person(){};new Person() instanceof Person;[] instanceof Object;// truenew Date() instanceof Object;// truenew Person instanceof Object;// true |
我们发现,虽然 instanceof 能够判断出 [ ] 是Array的实例,但它认为 [ ] 也是Object的实例,为什么呢?
我们来分析一下 [ ]、Array、Object 三者之间的关系:
从 instanceof 能够判断出 [ ].__proto__ 指向 Array.prototype,而 Array.prototype.__proto__ 又指向了Object.prototype,最终 Object.prototype.__proto__ 指向了null,标志着原型链的结束。因此,[]、Array、Object 就在内部形成了一条原型链:

从原型链可以看出,[] 的 __proto__ 直接指向Array.prototype,间接指向 Object.prototype,所以按照 instanceof 的判断规则,[] 就是Object的实例。依次类推,类似的 new Date()、new Person() 也会形成一条对应的原型链 。因此,instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型。
instanceof 操作符的问题在于,它假定只有一个全局执行环境。如果网页中包含多个框架,那实际上就存在两个以上不同的全局执行环境,从而存在两个以上不同版本的构造函数。如果你从一个框架向另一个框架传入一个数组,那么传入的数组与在第二个框架中原生创建的数组分别具有各自不同的构造函数。
|
1
2
3
4
5
|
var iframe = document.createElement('iframe');document.body.appendChild(iframe);xArray = window.frames[0].Array;var arr =new xArray(1,2,3);// [1,2,3]arr instanceof Array;// false |
针对数组的这个问题,ES5 提供了 Array.isArray() 方法 。该方法用以确认某个对象本身是否为 Array 类型,而不区分该对象在哪个环境中创建。
|
1
2
3
|
if (Array.isArray(value)){ //对数组执行某些操作} |
Array.isArray() 本质上检测的是对象的 [[Class]] 值,[[Class]] 是对象的一个内部属性,里面包含了对象的类型信息,其格式为 [object Xxx] ,Xxx 就是对应的具体类型 。对于数组而言,[[Class]] 的值就是 [object Array] 。
3. constructor
constructor 属性返回对创建此对象的数组函数的引用。
在JavaScript中,每个具有原型的对象都会自动获得constructor属性。
例:
以下代码中的[native code]
,表示这是JavaScript的底层内部代码实现,无法显示代码细节。

// String
var str = "字符串";
alert(str.constructor); // function String() { [native code] }
alert(str.constructor === String); // true // Array
var arr = [1, 2, 3];
alert(arr.constructor); // function Array() { [native code] }
alert(arr.constructor === Array); // true // Number
var num = 5;
alert(num.constructor); // function Number() { [native code] }
alert(num.constructor === Number); // true

4. prototype
以上三种方法多少都会有一些不能判断的情况。为了保证兼容性,可以通过Object.prototype.toString方法,判断某个对象值属于哪种内置类型。
需要注意区分大小写。
alert(Object.prototype.toString.call(“字符串”) === ‘[object String]’) -------> true;
alert(Object.prototype.toString.call(123) === ‘[object Number]’) -------> true;
alert(Object.prototype.toString.call([1,2,3]) === ‘[object Array]’) -------> true;
alert(Object.prototype.toString.call(new Date()) === ‘[object Date]’) -------> true;
alert(Object.prototype.toString.call(function a(){}) === ‘[object Function]’) -------> true;
alert(Object.prototype.toString.call({}) === ‘[object Object]’) -------> true;
判断JS的数据类型的更多相关文章
- 判断js中数据类型 的最短代码
判断字符串类型的: function isString(obj) { return obj+"" === obj; } 判断bool类型的: function isBool(obj ...
- 如何判断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中的数据类型(转)
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- 判断js中的数据类型
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- Js中数据类型判断的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- 判断js中的数据类型的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- 转:判断js中的数据类型的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
随机推荐
- 19/03/13python学习笔记
1.变量命名 name1 = 1 name2 = "sunj" 2.命名变量的规则 (1.变量是字母.数字.下划线的组合(2.不能以数字开头(3.不能用关键词命名变量(4.变量中间 ...
- Win下更新pip出现OSError:[WinError17]与PerrmissionError:[WinError5]及解决
环境:Win7 64位,python3.6.0 我在准备用pip装东西的时候,在cmd里先更新了一下pip,大概是9.0.1更新到9.0. 尝试更新pip命令: pip install --upgra ...
- c# android 全局捕获未处理异常
[Application] public class MyApp : Application { public MyApp(IntPtr javaReference, JniHandleOwnersh ...
- Spring IOC 相关的面试题
Spring最基础的部分就是IOC,对IOC的理解程度从某个方面代表着你对Spring 的理解程度,看了网上的一些面试题,针对Spring IOC相关的重点是下面几个: 1.Spring中Bean ...
- Linux(CentOS 7.0)安装Oracle11g R2
// 注释 # root用户 $oracle用户 1. 关闭安全措施 # chkconfig iptables off // 永久关闭防火墙 # serviceiptables stop // ...
- springMVC配置文件web.xml与spring-servlet.xml与spring-jdbc.xml与logback.xml与redis.properties与pom.xml
springMVC注解:@Controller @Service @Repository 分别标注于web层,service层,dao层. web.xml <?xml version=" ...
- RHEL7调图形化
RHEL7调图形化 作者:Eric 微信:loveoracle11g 1.将默认的运行级别修改为"多用户,无图模式" [root@server ~]# ln -sf /lib/sy ...
- 实验三:xen环境下的第一个虚拟机的安装
实验名称: xen环境下的第一个虚拟机的安装 实验环境: 我们这里继续上面实验二来完成这个实验: 环境则是xen的安装环境,如下图: 开启虚拟机的的硬件辅助虚拟化功能: 实验要求: 这里我们通过安装b ...
- 字符串String的API
字符串的理解 1. 字符串的属性 str.length 2. 字符串的方法 charAt() charCodeAt() indexOf() lastIndexOf() slice() substr ...
- Zookeeper的一致性协议:Zab
Zookeeper使用了一种称为Zab(Zookeeper Atomic Broadcast)的协议作为其一致性复制的核心,据其作者说这是一种新发算法,其特点是充分考虑了Yahoo的具体情况: ...