Object.prototype.__proto__

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

Revision Content

{{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 null (in the case of Object.prototype.__proto__). 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 webites 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

See also

Revision Source

<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 <code>null </code>(in the case of <code>Object.prototype.__proto__</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 webites 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>
<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