I ran into a strange snippet of code, which I cannot understand at all, here it is:
var obj = function() {};
obj.prototype.x = 5;
var instance1 = new obj();
obj.prototype = {y: 6};
var instance2 = new obj();
console.log(instance1.x, instance1.y, instance2.x, instance2.y);
// 5, undefined, undefined, 6
Now, the questions are:
5, undefined, undefined, 6
instead of undefined, 6, undefined, 6
?Every explanation is appreciated.
The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array.
__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new : ( new Foo ).
A prototype is just an object in memory. Every object has a prototype or parent except one object. We will refer to that object throughout the article as the base object. So Base Object doesn't have any prototype.
getPrototypeOf() The Object. getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.
Explanation
So first, your two lines of code create a function, obj
, and assign to it's prototype {x: 5}
.
When you create an instance of this object, it seems to have an internal reference to the prototype that existed when it was new
'd.
After this, you reassign the prototype to {y: 6}
which does not affect the instance1
internal reference to the first prototype.
Then when you create instance2
it has an internal reference to the 2nd prototype and therefore, logging them will produce 5, undefined, undefined, 6
.
#4
You could, rather than reassign the prototype to a new object:
obj.prototype = {y: 6};
Modify the prototype instead:
delete obj.prototype.x; // Setting to undefined should produce same behaviour
obj.prototype.y = 6;
This will produce the output: undefined, 6, undefined, 6
I have tested this with http://jsfiddle.net/9j3260gp/ on Chrome and Firefox latest versions on Windows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With