I understand javascript prototype inheritance through the __proto__ property. However I notice that when I do var f = function() {} f will now have a prototype property in addition to the __proto__ property. It would seem that prototype does not participate in property chaining. What exactly does it do?
I understand javascript prototype inheritance through the __proto__ property. However I notice that when
Share
It gets assigned as the prototype of objects created by using that function via the
newkeyword.So for instance:
The reference between
objand the object assigned toFoo.prototypeis a live one, and so adding further things toFoo.prototypewill make them show up onobj‘s prototype:Naturally, if you replace
Foo.prototype(which I discourage), then you’re pointingFoo.prototypeat a different object.objwill still refer to the old one:Gratuitous live example
Regarding
__proto__:__proto__is non-standard. Prior to ECMAScript5 (which is just a year and a half old), there was no standard way to interact directly with an object’s prototype, you could only assign them at object creation time, and only indirectly via a constructor function’sprototypeproperty.__proto__is a proprietary extension in some JavaScript engines (most notably Mozilla’s SpiderMonkey, the engine in Firefox). It’s not in any standard, and according to the ECMAScript committee, it won’t be. (Instead, ECMAScript5 provides functions for interacting with an object’s prototype.)__proto__is now deprecated by Mozilla.