I know that the prototype property of JavaScript function objects is copied to the internal [[Prototype]] (aka __proto__ ) property of objects created using the function as a constructor. Thus, I can set this property for any object that I want to use as a prototype:
function Foo() {} Foo.prototype = { toString: function() { return "I'm a Foo!"; } } new Foo().toString()
It seems like a widespread practice is to add functions that should act as class methods for an existing prototype of newly created functions, such as:
function Bar() {} Bar.prototype.toString = function() { return "I'm a Bar!"; } new Bar().toString()
However, it is not clear to me what the initial value of the prototype property is.
function Baz() {} Baz.prototype
The comment shows how the Chrome JavaScript console prints. Does this mean that every function I create actually creates two objects? One for the function itself ( constructor ) and one for its prototype?
Is it defined somewhere in the ECMAScript standard? I tried to find him, but could not. Do all browsers handle this the same way?
javascript prototype
Mforster
source share