[英]JavaScript: instanceof operator
First code: 第一个代码:
function MyConstructor() {}
var myobject = new MyConstructor();
MyConstructor.prototype = {};
[ myobject instanceof MyConstructor, // false - why?
myobject.constructor == MyConstructor, // true
myobject instanceof Object ] // true
even though MyConstructor.prototype
is replaced myobject
still inherits the properties from Myconstructor.prototype
. 即使MyConstructor.prototype
被替换myobject
依然继承从属性Myconstructor.prototype
。 So why is myobject instanceOf Myconstuctor
false? 那么为什么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)
second: 第二:
function MyConstructor() {}
MyConstructor.prototype = {};
var myobject = new MyConstructor();
myobject.constructor == MyConstructor; // false (accepted )
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. 因此,如果myobject.constructor是Object,为什么第一个:示例没有指向它, myobject.constructor
如何仍然指向MyConstructor
因为Myconstructor.prototype
在第一个示例中已经更改。
Can you clarify this please? 你能澄清一下吗?
even though MyConstructor.prototype is replaced myobject still inherits the properties from Myconstructor.prototype. 即使替换了MyConstructor.prototype,myobject仍会继承Myconstructor.prototype中的属性。
No. It inherits from the old object which was replaced. 不。它继承了被替换的旧物体。 And since that object is !== MyConstructor.prototype
, the instanceof
operator will yield false. 由于该对象是!== MyConstructor.prototype
,因此instanceof
运算符将产生false。 In your second example, myobject
inherits from the new prototype (the current MyConstructor.prototype
), and that's what instanceof
tells you. 在第二个示例中, myobject
继承自新原型(当前的MyConstructor.prototype
),这就是instanceof
告诉您的内容。
So if myobject.constructor 所以如果是myobject.constructor
The constructor
property is completely unrelated to instanceof
. 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.