Object.prototype.__proto__

  • Revision slug: JavaScript/Reference/Global_Objects/Object/proto
  • Revision title: __proto__
  • Revision id: 327149
  • Created:
  • Creator: Robg1
  • Is current revision? No
  • Comment Update and simplify description. There was a lot of content about prototype inheritance that is covered elsewhere.

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.

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

An object's __proto__ can be assigned a new value after construction, which also changes the [[Prototype]] and hence affects the object's inherited properties:

// 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/JavaScript/Reference/Global_Objects/Object/GetPrototypeOf" title="en/JavaScript/Reference/Global Objects/Object/GetPrototypeOf">Object.getPrototypeOf</a></code>.</p>
<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>
<p><code>// Declare a function to be used as a constructor function Employee() { /* initialise instance */ } // Create a new instance of Employee<br />
  var fred = new Employee();<br />
  // Test equivalence<br />
  fred.__proto__ === Employee.prototype; // true</code></p>
<p>An object's <code>__proto__</code> can be assigned a new value after construction, which also changes the <code>[[Prototype]]</code> and hence affects the object's inherited properties:</p>
<pre>
<code class="brush: js">// Assign a new object to __proto__
fred.__proto__ = Object.prototype</code>;</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/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 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