1

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?

0

1 Answer 1

4

even though MyConstructor.prototype is replaced myobject still inherits the properties from 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. In your second example, myobject inherits from the new prototype (the current MyConstructor.prototype), and that's what instanceof tells you.

So if myobject.constructor

The constructor property is completely unrelated to 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
Sign up to request clarification or add additional context in comments.

8 Comments

oo that way can we set the construtor property?really good post
You can set nearly every property (there are only a few non-writable ones).
u made mistake setting oldProto.constructor==Constructor ,it should be oldInstance.constructor==constructor
I did not set it. And no, both expressions are true
as per your reply if i do {}.constructor="foo" and check {}.constructor ,the result is again the same Object why so?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.