Object.prototype.__proto__

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

Revision Content

See the approved Object.setPrototypeOf for a better support.
 
{{Non-standard_header}}

An Object's __proto__ property references the same object as its internal [[Prototype]] (often referred to as "the prototype"), which may be an object or, as in the default case of Object.prototype.__proto__, null . This property is an abstraction error, because a property with the same name, but some other value, could be defined on the object too. If there is a need to reference an object's prototype, the preferred method is to use Object.getPrototypeOf.

Property of Object
Implemented in JavaScript ?
ECMAScript Edition -

A __proto__ pseudo property has been included in §B.3.1 of the draft ECMAScript ed. 6 specification (note that the specification codifies what is already in implementations and what websites may currently rely on).

Syntax

var proto = obj.__proto__;

Note: that 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 reference the same object as its internal [[Prototype]] (i.e. its constructor's prototype object).  Assigning a new value to __proto__ also changes the value of the internal [[Prototype]] property, except where the object is non–extensible.

To understand how prototypes are used for inheritance, see the MDN article Inheritance and the prototype chain.

Example

In the following, a new instance of Employee is created, then tested to show that its __proto__ is the same object as its constructor's prototype.

// Declare a function to be used as a constructor
function Employee() {
  /* initialise instance */
}

// Create a new instance of Employee
var fred = new Employee();

// Test equivalence
fred.__proto__ === Employee.prototype; // true

At this point, fred inherits from Employee, however assigning a different object to fred.__proto__ can change that:

// Assign a new object to __proto__
fred.__proto__ = Object.prototype;

Now fred no longer inherits from Employee.prototype, but directly from Object.prototype, and loses the properties it originally inherited from Employee.prototype.

However, this only applies to extensible objects, a non–extensible object's __proto__ property cannot be changed:

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

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

Note that even Object.prototype's __proto__ property can be redefined as long as the chain leads to null:

var b = {};

Object.prototype.__proto__ = {
    hi: function () {alert('hi');},
    __proto__: null
};

 
b.hi();

If Object.prototype's __proto__ had not been set to null, or had not been set to another object whose prototype chain did not eventually lead explicitly to null, a "cyclic __proto__ value" TypeError would result since the chain must eventually lead to null (as it normally does on Object.prototype).

See also

Revision Source

<div>
  See the approved&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf" title="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf">Object.setPrototypeOf</a>&nbsp;for a better support.</div>
<div>
  &nbsp;</div>
<div>
  {{Non-standard_header}}</div>
<p>An Object's <code>__proto__</code> property references the same object as its internal <code>[[Prototype]]</code> (often referred to as "the prototype"), which may be an object or, as in the default case of <code>Object.prototype.__proto__,</code> <code>null&nbsp;</code>. This property is an abstraction error, because a property with the same name, but some other value, could be defined on the object too. If there is a need to reference an object's prototype, the preferred method is to use <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/GetPrototypeOf" title="JavaScript/Reference/Global Objects/Object/GetPrototypeOf">Object.getPrototypeOf</a></code>.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Property of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript ?</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>-</td>
    </tr>
  </tbody>
</table>
<p>A <code>__proto__</code> pseudo property has been included in §B.3.1 of the draft ECMAScript ed. 6 specification (note that the specification codifies what is already in implementations and what websites may currently rely on).</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>var proto = <em>obj</em>.__proto__;</code></pre>
<p>Note: that 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 reference the same object as its internal <code>[[Prototype]]</code> (i.e. its constructor's <code>prototype</code> object).&nbsp; Assigning a new value to <code>__proto__</code> also changes the value of the internal <code>[[Prototype]]</code> property, except where the object is non–extensible.</p>
<p>To understand how prototypes are used for inheritance, see the MDN article <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain" title="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain">Inheritance and the prototype chain</a>.</p>
<h2 id="Example" name="Example">Example</h2>
<p>In the following, a new instance of <code>Employee</code> is created, then tested to show that its <code>__proto__</code> is the same object as its constructor's <code>prototype</code>.</p>
<pre class="brush: js">
// Declare a function to be used as a constructor
function Employee() {
  /* initialise instance */
}

// Create a new instance of Employee
var fred = new Employee();

// Test equivalence
fred.__proto__ === Employee.prototype; // true
</pre>
<p>At this point, <code>fred</code> inherits from <code>Employee</code>, however assigning a different object to <code>fred.__proto__</code> can change that:</p>
<pre class="brush: js">
// Assign a new object to __proto__
fred.__proto__ = Object.prototype;
</pre>
<p>Now <code>fred</code> no longer inherits from <code>Employee.prototype</code>, but directly from <code>Object.prototype</code>, and loses the properties it originally inherited from <code>Employee.prototype</code>.</p>
<p>However, this only applies to <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/isExtensible" title="JavaScript/Reference/Global Objects/Object/isExtensible">extensible</a> objects, a non–extensible object's&nbsp;<code style="color: inherit; font-weight: inherit; ">__proto__</code>&nbsp;property cannot be changed:</p>
<pre class="brush: js">
var obj = {};
Object.preventExtensions(obj);

obj.__proto__ = {}; // throws a TypeError
</pre>
<p name="See_also">Note that even <code>Object.prototype</code>'s <code>__proto__</code> property can be redefined as long as the chain leads to null:</p>
<pre class="brush: js" name="See_also">
var b = {};

Object.prototype.__proto__ = {
&nbsp;&nbsp;&nbsp; hi: function () {alert('hi');},
&nbsp;&nbsp;&nbsp; __proto__: null
};

&nbsp;
b.hi();
</pre>
<p class="brush: js" name="See_also">If <code>Object.prototype</code>'s <code>__proto__</code> had not been set to <code>null</code>, or had not been set to another object whose prototype chain did not eventually lead explicitly to <code>null</code>, a "cyclic __proto__ value" TypeError would result since the chain must eventually lead to <code>null</code> (as it normally does on <code>Object.prototype</code>).</p>
<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>
  <li><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain" title="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain">Inheritance and the prototype chain</a></li>
  <li><a href="http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts" title="http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts">Ecma–262 Edition 6 drafts</a></li>
</ul>
Revert to this revision