admin管理员组

文章数量:1336660

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 widespread 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 ment 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?

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 widespread 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 ment 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?

Share Improve this question asked Nov 1, 2010 at 22:38 MForsterMForster 9,3865 gold badges32 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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 pletely-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.

本文标签: What is the initial value of a JavaScript function39s prototype propertyStack Overflow