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. 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)
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.
Can you clarify this please?
No. It inherits from the old object which was replaced. And since that object is
!== MyConstructor.prototype, theinstanceofoperator will yield false. In your second example,myobjectinherits from the new prototype (the currentMyConstructor.prototype), and that’s whatinstanceoftells you.The
constructorproperty is completely unrelated toinstanceof.