Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 885
MForster Avatar asked Nov 01 '10 22:11

MForster


1 Answers

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.

like image 180
bobince Avatar answered Sep 21 '22 02:09

bobince