What is the JS function prototype property used for?

I understand javascript prototype inheritance through property __proto__. However, I notice that when I var f = function() {}f will now have a property prototypein addition to the property __proto__. It would seem that it is prototypenot involved in the chain of properties. What exactly is he doing?

+2
source share
2 answers

It is assigned as a prototype of objects created using this function using a keyword new.

So for example:

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

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

obj , Foo.prototype, , Foo.prototype obj:

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

, Foo.prototype ( ), Foo.prototype . obj :

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

__proto__: __proto__ . ECMAScript5 ( ) , prototype. __proto__ JavaScript- ( Mozilla SpiderMonkey, Firefox). - , , ECMAScript, . ( ECMAScript5 .) __proto__ Mozilla.

+4

_proto_ (, ), . , , Object.getPrototypeOf(ref): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/Proto

prototype, , ( ) . : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/prototype

( new), , prototype. (var myNewObject = new Foo()) Object.getPrototypeOf(myNewObject) , .

: __proto__ , - IS, prototype - , ( ).

+4

All Articles