typeof和instanceof

问题描述与答案

求下面表达式的结果
[typeof null, null instanceof Object]
答案
[“object”, false]

答案解析

这里有两个知识点typeof和instanceof

typeof

  • 用于判断数据类型
  • typeof可能返回的值有:
类型结果
Undefined“undefined”
布尔值“boolean”
null,array,object以及函数实例(new + 函数),任意内置对象(非函数)“object”
任意数字或者NaN“number”
BigInt(任意精度格式的整数)“bigint”
字符串“string”
符号类型值“symbol”
函数类型“function”
宿主对象(JS引擎内置对象,而不是DOM或者其他提供的)由编译器各自实现的字符串,但不是"undefined",“number”,“boolean”,“number”,“string”。
正则表达式各浏览器表现不一
  • 所以typeof null的值为"object"

instanceof

  • instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。(直白点说就是判断左边的值是否是右边对象的实例)
    其语法是object instanceof constructor
    同样可用于判断数据类型

  • instanceof 左操作数是一个类,右操作数是标识对象的类。如果左侧的对象是右侧类的实例,则返回true.而js中对象的类是通过初始化它们的构造函数来定义的。即instanceof的右操作数应当是一个函数。所有的对象都是object的实例。如果左操作数不是对象,则返回false,如果右操作数不是函数,则抛出typeError。

  • 有一点需要注意,instanceof 用来比较属于不同 JavaScript 上下文的对象(比如,浏览器中不同的文档结构)时将会出错, 因为它们的构造函数不会是同一个对象。

  • 关于instanceof,有以下示例,供参考

    instanceof 比较自定义对象

    function Foo() {}
    function Bar() {}
    Bar.prototype = new Foo();
    new Bar() instanceof Bar; // true
    new Bar() instanceof Foo; // true
    // 如果仅仅设置 Bar.prototype 为函数 Foo 本身,而不是 Foo 构造函数的一个实例
    Bar.prototype = Foo;
    new Bar() instanceof Foo; // false
    

    instanceof 比较内置类型

    new String('foo') instanceof String; // true
    new String('foo') instanceof Object; // true
    'foo' instanceof String; // false
    'foo' instanceof Object; // false
    

    instanceof其他示例

    function C(){} // defining a constructor
    function D(){} // defining another constructor
    var o = new C();
    o instanceof C; // true, because: Object.getPrototypeOf(o) === C.prototype
    o instanceof D; // false, because D.prototype is nowhere in o's prototype chain
    o instanceof Object; // true, because:
    C.prototype instanceof Object // true
    C.prototype = {};
    var o2 = new C();
    o2 instanceof C; // true
    o instanceof C; // false, because C.prototype is nowhere in o's prototype chain anymore
    D.prototype = new C(); // use inheritance
    var o3 = new D();
    o3 instanceof D; // true
    o3 instanceof C; // true
    var myString = new String();
    var myDate = new Date();
    myString instanceof String; // returns true
    myString instanceof Object; // returns true
    myString instanceof Date;  // returns false
    myDate instanceof Date;   // returns true
    myDate instanceof Object;  // returns true
    myDate instanceof String;  // returns false
    function Car(make, model, year) {
     this.make = make;
     this.model = model;
     this.year = year;
    }
    var mycar = new Car("Honda", "Accord", 1998);
    var a = mycar instanceof Car;  // returns true
    var b = mycar instanceof Object; // returns true
    

    null代表空值,其并非是Object原型链中的产物,因此null instanceof Object为false

文章参考:
详解JavaScript中typeof与instanceof用法
JavaScript中遇到“null instanceof Object ”返回值为false的解释

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值