Object.setPrototypeOf()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/setPrototypeOf
  • Revision title: setPrototypeOf
  • Revision id: 411431
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment

Revision Content

Summary

Set the prototype (i.e. the internal [[Prototype]]) of a specified object to another object or null.

Method of Object
Implemented in unImplemented
ECMAScript Edition ECMAScript 6th Edition

Syntax

Object.setPrototypeOf(object, prototype)

Parameters

object
The object whose prototype is to be  set.
prototype
The object's new prototype(object or null).

Description

Throws a TypeError exception if the prototype parameter isn't an Object or null(i.e. Number, String, Boolean, undefined).

Notes

Object.setPrototypeOf() is in the latest ECMAScript 6 standard draft.the old object.__proto__ property is not standard and has been deprecated from the latest ES6 draft,but all the modern browsers(include IE11 preview version) has already implemented it.

Polyfill

Object.setPrototypeOf = function (obj, proto) {
  obj.__proto__ = proto;
};

Example

var dict = Object.setPrototypeOf({},null);

Cross-browser compatibility

{{CompatibilityTable}}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Set the prototype (i.e. the internal <code>[[Prototype]]</code>) of a specified object to another object or null.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method 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>unImplemented</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 6th Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>Object.setPrototypeOf(<em>object, prototype</em>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    object</dt>
  <dd>
    The object whose prototype is to be&nbsp; set.</dd>
  <dt>
    prototype</dt>
  <dd>
    The object's new prototype(object or null).</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>Throws a <a href="/en-US/docs/JavaScript/Reference/Global_Objects/TypeError" title="JavaScript/Reference/Global Objects/TypeError"><code>TypeError</code></a> exception if the prototype parameter isn't an <code>Object</code> or null(i.e. Number, String, Boolean, undefined).</p>
<h2 id="Notes" name="Notes">Notes</h2>
<p><code>Object.setPrototypeOf()</code> is in the latest ECMAScript 6 standard draft.the old <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/proto" title="JavaScript/Reference/Global_Objects/Object/proto"><code><em>object</em>.__proto__</code></a> property is not standard and has been deprecated from the latest ES6 draft,but all the modern browsers(include IE11 preview version) has already implemented it.</p>
<h2 id="Notes" name="Notes">Polyfill</h2>
<pre class="brush: js">
Object.setPrototypeOf = function (obj, proto) {
  obj.__proto__ = proto;
};</pre>
<h2 id="Notes" name="Notes">Example</h2>
<pre class="brush: js">
var dict = Object.setPrototypeOf({},null);
</pre>
<h2 id="Cross-browser_compatibility">Cross-browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox (Gecko)</th>
        <th>Chrome</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>Android</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/isPrototypeOf" title="JavaScript/Reference/Global Objects/Object/IsPrototypeOf">isPrototypeOf</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getPrototypeOf" title="JavaScript/Reference/Global Objects/Object/IsPrototypeOf">getPrototypeOf</a></li>
</ul>
Revert to this revision