mozilla

Revision 47680 of Object.prototype.__proto__

  • Revision slug: JavaScript/Reference/Global_Objects/Object/proto
  • Revision title: __proto__
  • Revision id: 47680
  • Created:
  • Creator: hdon
  • Is current revision? No
  • Comment edited info about what objects have a __proto__ property; 10 words added, 3 words removed

Revision Content

{{ Non-standard_header() }}

Summary

A link in the prototype chain of every object except Object.prototype, whose __proto__ property has the value null; used to implement prototype-inheritance based property lookup.

Syntax

obj.__proto__

Note, this is two underscores before and after the five charactars 'proto'.

Description

When an object is created, its __proto__ property is set to constructing function's prototype property. For example var fred = new Employee(); will cause fred.__proto__ = Employee.prototype;.

This is used at runtime to look up properties which are not declared in the object directly. E.g. when fred.doSomething() is executed and fred does not contain a doSomething, fred.__proto__ is checked, which points to Employee.prototype, which contains a doSomething, i.e. fred.__proto__.doSomething() is invoked.

Note that __proto__ is a property of the instances, whereas prototype is a property of their constructor functions.

Example

This example demonstrates that the __proto__ property can be change to point to a different object after initial construction. This change will alter the lookup results for object properties. This example also illustrates that all objects have __proto__, including the objects bound to the prototype property of functions. The object anOnion will have a __proto__ property equal to Plant.prototype; if we write anOnion.foo, then we will lookup foo in the anOnion object first, then in Plant.prototype (the value of anOnion.__proto__), then in Lifeform.prototype (the value of Plant.prototype set by the call to extend()), and finally in Lifeform.__proto__.

function extend(child, supertype)
{
   child.prototype.__proto__ = supertype.prototype;
}

extend(Animal, Lifeform);
extend(Plant, Lifeform);

var anOnion = new Plant();

Revision Source

<p>{{ Non-standard_header() }}</p>
<h3 name="Summary">Summary</h3>
<p>A link in the prototype chain of every object except Object.prototype, whose <code>__proto__</code> property has the value <code>null</code>; used to implement prototype-inheritance based property lookup.</p>
<h3 name="Syntax">Syntax</h3>
<p><code><em>obj</em>.__proto__</code></p>
<p><code>Note, this is two underscores before and after the five charactars 'proto'.</code></p>
<h3 name="Description">Description</h3>
<p>When an object is created, its __proto__ property is set to constructing function's <code>prototype</code> property. For example <code>var fred = new Employee();</code> will cause <code>fred.__proto__ = Employee.prototype;</code>.</p>
<p>This is used at runtime to look up properties which are not declared in the object directly. E.g. when <code>fred.doSomething()</code> is executed and <code>fred</code> does not contain a <code>doSomething</code>, <code>fred.__proto__</code> is checked, which points to <code>Employee.prototype</code>, which contains a <code>doSomething</code>, i.e. <code>fred.__proto__.doSomething()</code> is invoked.</p>
<p>Note that <code><em>__proto__</em></code> is a property of the instances, whereas <code>prototype</code> is a property of their constructor functions.</p>
<h3 name="Example">Example</h3>
<p>This example demonstrates that the <code>__proto__</code> property can be change to point to a different object after initial construction. This change will alter the lookup results for object properties. This example also illustrates that all objects have <code>__proto__</code>, including the objects bound to the <code>prototype</code> property of functions. The object <code> anOnion</code> will have a <code>__proto__</code> property equal to Plant.prototype; if we write <code>anOnion.foo</code>, then we will lookup <code>foo</code> in the anOnion object first, then in <code>Plant.prototype</code> (the value of <code>anOnion.__proto__</code>), then in <code>Lifeform.prototype</code> (the value of <code>Plant.prototype</code> set by the call to <code>extend</code>()), and finally in <code>Lifeform.__proto__</code>.</p>
<pre class="eval">function extend(child, supertype)
{
   child.prototype.__proto__ = supertype.prototype;
}

extend(Animal, Lifeform);
extend(Plant, Lifeform);

var anOnion = new Plant();
</pre>
Revert to this revision