美文网首页
isPrototypeOf 和instanceof辨别

isPrototypeOf 和instanceof辨别

作者: 欢欢小天使K | 来源:发表于2020-04-13 17:42 被阅读0次

isPrototypeOf

语法:prototypeObj.isPrototypeOf(object)
解释:表示调用对象是否在另一个对象的原型链上。

instanceof

语法:object instanceof constructor
解释:instanceof运算符用来测试一个对象在其原型链中是否存在一个构造函数的prototype属性

var p = { name: 'o4'};
    var o4 = Object.create(p)
//undefined
o4 instanceof p.constructor
//true
p.constructor.prototype.isPrototypeOf(o4)
//true
由于继承作用,导致:
o4 instanceof Object
//true
Object.prototype.isPrototypeOf(p)
//true

相关文章

网友评论

      本文标题:isPrototypeOf 和instanceof辨别

      本文链接:https://www.haomeiwen.com/subject/zshnmhtx.html