JavaScript:instanceof运算符 [英] JavaScript: instanceof operator

查看:49
本文介绍了JavaScript:instanceof运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个代码:

function MyConstructor() {}
var myobject = new MyConstructor();
MyConstructor.prototype = {};

[ myobject instanceof MyConstructor,   // false - why?
myobject.constructor == MyConstructor, // true
myobject instanceof Object ]           // true

即使替换了MyConstructor.prototypemyobject仍然继承了Myconstructor.prototype的属性.那么myobject instanceOf Myconstuctor为什么是假的?

even though MyConstructor.prototype is replaced myobject still inherits the properties from Myconstructor.prototype. So why is myobject instanceOf Myconstuctor false?

function MyConstructor() {}
MyConstructor.prototype = {};
var myobject = new MyConstructor();
myobject instanceof MyConstructor  // true (it is because myobject still inherits from
                                   // Myconstructor.prototype although it has been replaced)

秒:

 function MyConstructor() {}
 MyConstructor.prototype = {};
 var myobject = new MyConstructor();

 myobject.constructor == MyConstructor;  // false (accepted )

因此,如果myobject.constructor是Object,为什么第一个示例没有指向它,为什么myobject.constructor仍然指向MyConstructor,因为在第一个示例中Myconstructor.prototype已更改.

So if myobject.constructor is Object why the first: example not pointing it, how can the myobject.constructor still points to MyConstructor since Myconstructor.prototype has changed in first example.

请您澄清一下吗?

推荐答案

即使替换了MyConstructor.prototype,myobject仍然继承了Myconstructor.prototype的属性.

even though MyConstructor.prototype is replaced myobject still inherits the properties from Myconstructor.prototype.

不.它继承自被替换的旧对象.并且由于该对象是!== MyConstructor.prototype,因此 instanceof运算符将产生false.在第二个示例中,myobject继承自新原型(当前的MyConstructor.prototype),而instanceof就是这样.

No. It inherits from the old object which was replaced. And since that object is !== MyConstructor.prototype, the instanceof operator will yield false. In your second example, myobject inherits from the new prototype (the current MyConstructor.prototype), and that's what instanceof tells you.

因此,如果myobject.constructor

So if myobject.constructor

constructor属性与instanceof完全无关.

function Constructor() {}
var oldProto = Constructor.prototype;
var oldInstance = new Constructor();

Constructor.prototype = {constructor:"something else"};
var newProto = Constructor.prototype;
var newInstance = new Constructor();

// all these are true:
Object.getPrototypeOf(oldInstance) === oldProto;
Object.getPrototypeOf(newInstance) == newProto;
oldProto !== newProto;
oldProto.constructor === Constructor; // was set implicitly on creating the function
oldInstance.constructor === oldProto.constructor; // inherited
newProto.constructor === "something else"; // if not explicitly set, comes from Object.prototype
newInstance.constructor === newProto.constructor; // inherited

Constructor.prototype === newProto;
newInstance instanceof Constructor; // because the above

Constructor.prototype !== oldProto;
! (oldInstance instanceof Constructor) // because the above

这篇关于JavaScript:instanceof运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆