mozilla

Revision 316339 of Object.prototype.__proto__

  • Revision slug: JavaScript/Reference/Global_Objects/Object/proto
  • Revision title: __proto__
  • Revision id: 316339
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{ Non-standard_header() }}

Refers to the prototype of the object, which may be an object or null. This property is an abstraction error, because a property with the same name, but any other value could be defined too. If you only need to retrieve the prototype, please prefer Object.getPrototypeOf instead.

In all likelyhood, __proto__ will be in ECMAScript 6 defined as a de-facto standard (the specification codifies what is already in implementations and what webites in the wild rely on).

Property of ???
Implemented in ???
ECMAScript Edition none

Syntax

var proto = obj.__proto__;

Note: this is two underscores, followed by the five characters "proto", followed by two more underscores.

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 changed 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();

However, this only applies to extensible objects: a non-extensible object's __proto__ property (more generally, the object's prototype) cannot be changed:

var obj = {};
Object.preventExtensions(obj);

obj.__proto__ = {}; // throws a TypeError

See also

Revision Source

<div>
  {{ Non-standard_header() }}</div>
<p>Refers to the prototype of the object, which may be an object or <code>null</code>. This property is an abstraction error, because a property with the same name, but any other value could be defined too. If you only need to retrieve the prototype, please prefer <code><a href="/en/JavaScript/Reference/Global_Objects/Object/GetPrototypeOf" title="en/JavaScript/Reference/Global Objects/Object/GetPrototypeOf">Object.getPrototypeOf</a></code> instead.</p>
<p>In all likelyhood, <code>__proto__</code> will be in ECMAScript 6 defined as a de-facto standard (the specification codifies what is already in implementations and what webites in the wild rely on).</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Property of ???</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>???</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>none</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>var proto = <em>obj</em>.__proto__;</code></pre>
<p>Note: this is two underscores, followed by the five characters "proto", followed by two more underscores.</p>
<h2 id="Description" name="Description">Description</h2>
<p>When an object is created, its <code>__proto__</code> 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>
<h2 id="Example" name="Example">Example</h2>
<p>This example demonstrates that the <code>__proto__</code> property can be changed 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 <code>Plant.prototype</code>; if we write <code>anOnion.foo</code>, then we will lookup <code>foo</code> in the <code>anOnion</code> 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><code>()</code>), and finally in <code>Lifeform.__proto__</code>.</p>
<pre class="brush: js">
function extend(child, supertype) {
   child.prototype.__proto__ = supertype.prototype;
}

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

var anOnion = new Plant();
</pre>
<p>However, this only applies to <a href="/en/JavaScript/Reference/Global_Objects/Object/isExtensible" title="en/JavaScript/Reference/Global Objects/Object/isExtensible">extensible</a> objects:&nbsp;a non-extensible object's&nbsp;<code style="color: inherit; font-weight: inherit; ">__proto__</code>&nbsp;property (more generally, the object's prototype) cannot be changed:</p>
<pre class="brush: js">
var obj = {};
Object.preventExtensions(obj);

obj.__proto__ = {}; // throws a TypeError
</pre>

<h2 id="See_also" name="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/Parent" title="__parent__ | JavaScript | MDN">__parent__</a></li>
</ul>
Revert to this revision