首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascript instanceOf解释

javascript instanceOf解释
EN

Stack Overflow用户
提问于 2013-03-19 04:05:14
回答 1查看 108关注 0票数 1

我只是在玩弄instanceof操作符。我想知道我的理解是否正确。

代码语言:javascript
复制
var C = function(){};
// in the above statement C has a "prototype" property which points to an object which has 
// the constructor property which points to C constructor
var o1 = new C();
// in above statement o1.__proto__ is points to C.prototype. that means inheriting from C.prototype.
console.log(o1 instanceof C) // returns true
//instanceof will check o1.__proto__ is equals to C.prototype(recursively until it finds null object).
C.prototype = {};
console.log(o1 instanceof C) // false;
in the above case o1 was inherited from C.prototype which points to the different object not the present C.prototype object (empty object). so the instanceof condition check fails hence its false.

如果我的解释有误,请告诉我

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-19 04:07:54

是的,instanceof检查对象的原型链中的构造函数,如果在链中的任何位置找到传递的构造函数,则返回true。因此,如果您销毁一个函数的原型,就像您用一个空对象覆盖它所做的那样,那么instanceof将总是返回false。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15485884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档