开发者

What does a JS function's prototype property used for?

开发者 https://www.devze.com 2023-03-11 21:04 出处:网络
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

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?


It gets assigned as the prototype of objects created by using that function via the new keyword.

So for instance:

function Foo() {
}
Foo.prototype.bar = 47;

var obj = new Foo();
alert(obj.bar); // alerts 47, via `obj`'s prototype

The reference between obj and the object assigned to Foo.prototype is a live one, and so adding further things to Foo.prototype will make them show up on obj's prototype:

Foo.prototype.charlie = 62;
alert(obj.charlie); // alerts 62

Naturally, if you replace Foo.prototype (which I discourage), then you're pointing Foo.prototype at a different object. obj will still refer to the old one:

Foo.prototype = {delta: 77}; // Not recommended
alert(obj.delta); // alerts "undefined"

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's prototype property. __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.


the _proto_ property is a property of an instance of an object (the object being, in this case, a function), it refers to prototype of the instance. You should note that this property is non-standard and depreciated, use Object.getPrototypeOf(ref) instead: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/Proto

prototype, in contrast, is a property of a function declaration (not an instance), and is the prototype for all instances of the function. Check out the docs: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/prototype

When you create a new instance of a function (using the new keyword), a function object is created, as defined by the prototype. After it is instantiated (var myNewObject = new Foo()), a call to Object.getPrototypeOf(myNewObject) will return a reference to the prototype upon which the instance is based.

To sum it up: __proto__ is what something IS, prototype is what something CAN BE (or might already be).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号