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;// true
new Date() instanceof Date;// true
 
function Person(){};
new Person() instanceof Person;
 
[] instanceof Object;// true
new Date() instanceof Object;// true
new 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的数据类型的更多相关文章

  1. 判断js中数据类型 的最短代码

    判断字符串类型的: function isString(obj) { return obj+"" === obj; } 判断bool类型的: function isBool(obj ...

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

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

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

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

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

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

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

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

  6. 判断js中的数据类型

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

  7. Js中数据类型判断的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  8. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  9. 转:判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

随机推荐

  1. windows下使用kafka的常用命令

    参考文档: https://blog.csdn.net/evankaka/article/details/52421314 http://orchome.com/6 1 启动zookeeper cmd ...

  2. 转"container of()函数简介"链接地址

    https://blog.csdn.net/s2603898260/article/details/79371024 提示关键字: 指针0的使用 typeof的使用

  3. s2第六章继承和多态

    public class Employee { //年龄 public int Age { get; set; } //性别 public Gender Gender { get; set; } // ...

  4. fromkeys()

    #fromkeys() #说明:用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值 #案例 d=[1,2,3] dict={} dict=dict.fromkeys( ...

  5. How to Create an PostgreSQL Extension

    转自:https://severalnines.com/blog/creating-new-modules-using-postgresql-create-extension Extensibilit ...

  6. 项目期复习:JS操作符,弹窗与调试,凝视,数据类型转换

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/huangyibin628/article/details/26364901 1.JS操作符 ① 除法 ...

  7. Django学习笔记之数据库-数据库与模型

    MySQL数据库 在网站开发中,数据库是网站的重要组成部分.只有提供数据库,数据才能够动态的展示,而不是在网页中显示一个静态的页面.数据库有很多,比如有SQL Server.Oracle.Postgr ...

  8. centos7下安全访问远程服务器

    1. 添加普通账号 众所周知,linux下的root拥有最高权限,可以执行任何命令.在使用root身份操作时,有时的一个不注意就可能将非常重要的删除(最可怕的是 rm -rf /).而linux不像w ...

  9. Memcached 使用备注

    ICSharpCode.SharpZipLib未能加载文件或程序集 Memcached使用的是0.84版本的dll,vs2013带的是0.86版本 在web.config中<runtime> ...

  10. Avalon Master 外设