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 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
prototypeon any newly-createdFunctioninstance is a new instance ofObject, but with the own-propertyconstructorset to point back to the new function.This is described in typical ECMAScript-spec completely-unreadable fashion by ECMA262-5 in section 13.2: