JS: Prototypes
In addition to their own properties, almost all JavaScript objects also have a prototype. A prototype is another object that is used as a fallback source of properties. The ancestral prototype, the entity behind all objects, is Object.prototype
.
Many objects don’t directly have Object.prototype
as their prototype, but instead have another object, which provides its own default properties. Such a prototype object will itself have a prototype, often Object.prototype
, so that it still indirectly provides methods like toString
. Functions derive from Function.prototype
and arrays derive from Array.prototype
.
var empty = {}
console.log(empty.toString) // -> function toString()...{}
console.log(empty.toString()) // -> [object Object]
Object.getPrototypeOf
function returns the prototype of an object. You can use Object.create
to create an object with a specific prototype.
var
...