开发者

What is the initial value of a JavaScript function's prototype property?

开发者 https://www.devze.com 2023-01-22 19:11 出处:网络
I know that the prototype property of JavaScript function objects is copied to the internal [[Prototype]] property (a.k.a. __proto__) of objects instantiated by using the function as a constructor. So

I know that the prototype property of JavaScript function objects is copied to the internal [[Prototype]] property (a.k.a. __proto__) of objects instantiated by using the function as a constructor. So, I can set this property to any object that I want to act as the prototype:

function Foo() {}
Foo.prototype = {
  toString: function() { return "I'm a Foo!"; }
}
new Foo().toString()
// --> "I'm a Foo!"

It seems that it is wides开发者_StackOverflowpread practice to add functions that should act as class methods to the existing prototype of newly generated functions like this:

function Bar() {}
Bar.prototype.toString = function() {
  return "I'm a Bar!";
}
new Bar().toString()
// --> "I'm a Bar!"

It is unclear to me, though, what the initial value of the prototype property is.

function Baz() {}
Baz.prototype
// --> Baz {
//       constructor: function Baz() {},
//       __proto__: Object
//     }

The comment shows what Chrome's JavaScript console prints. Does this mean that every function that I create actually creates two objects? One for the function itself (the constructor) and one for its prototype?

Is this defined somewhere in the ECMAScript standard? I tried to find it but couldn't. Do all browsers handle this in the same way?


The initial value of prototype on any newly-created Function instance is a new instance of Object, but with the own-property constructor set to point back to the new function.

This is described in typical ECMAScript-spec completely-unreadable fashion by ECMA262-5 in section 13.2:

(16.) Let proto be the result of creating a new object as would be constructed by the expression new Object() where Object is the standard built-in constructor with that name

(17.) Call the [[DefineOwnProperty]] internal method of proto with arguments "constructor", Property Descriptor {[[Value]]: F, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}, and false.

(18.) Call the [[DefineOwnProperty]] internal method of F with arguments "prototype", Property Descriptor {[[Value]]: proto, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false}, and false.

0

精彩评论

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