Object.defineProperties()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/defineProperties
  • Revision title: defineProperties
  • Revision id: 16163
  • Created:
  • Creator: fusionchess
  • Is current revision? No
  • Comment 2 words added

Revision Content

{{ js_minversion_header("1.8.5") }}

Summary

Defines new or modifies existing properties directly on an object, returning the object.

Method of Object
Implemented in JavaScript 1.8.5
ECMAScript Edition ECMAScript 5th Edition

Syntax

Object.defineProperties(obj, props)

Parameters

obj
The object on which to define or modify properties.
props
An object whose own enumerable properties constitute descriptors for the properties to be defined or modified.

Description

Object.defineProperties, in essence, defines all properties corresponding to the enumerable own properties of props on the object objrops object.

Example usage

Object.defineProperties(obj, {
  "property1": {
    value: true,
    writable: true
  },
  "property2": {
    value: "Hello",
    writable: false
  }
  // etc. etc.
});

Polyfill

Assuming a pristine execution environment with all names and properties referring to their initial values, Object.defineProperties is almost completely equivalent (note the comment in isCallable) to the following reimplementation in JavaScript:

function defineProperties(obj, properties)
{
  function convertToDescriptor(desc)
  {
    function hasProperty(obj, prop)
    {
      return Object.prototype.hasOwnProperty.call(obj, prop);
    }

    function isCallable(v)
    {
      // NB: modify as necessary if other values than functions are callable.
      return typeof v === "function";
    }

    if (typeof desc !== "object" || desc === null)
      throw new TypeError("bad desc");

    var d = {};
    if (hasProperty(desc, "enumerable"))
      d.enumerable = !!obj.enumerable;
    if (hasProperty(desc, "configurable"))
      d.configurable = !!obj.configurable;
    if (hasProperty(desc, "value"))
      d.value = obj.value;
    if (hasProperty(desc, "writable"))
      d.writable = !!desc.writable;
    if (hasProperty(desc, "get"))
    {
      var g = desc.get;
      if (!isCallable(g) && g !== "undefined")
        throw new TypeError("bad get");
      d.get = g;
    }
    if (hasProperty(desc, "set"))
    {
      var s = desc.set;
      if (!isCallable(s) && s !== "undefined")
        throw new TypeError("bad set");
      d.set = s;
    }

    if (("get" in d || "set" in d) && ("value" in d || "writable" in d))
      throw new TypeError("identity-confused descriptor");

    return d;
  }

  if (typeof obj !== "object" || obj === null)
    throw new TypeError("bad obj");

  properties = Object(properties);
  var keys = Object.keys(properties);
  var descs = [];
  for (var i = 0; i < keys.length; i++)
    descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);
  for (var i = 0; i < descs.length; i++)
    Object.defineProperty(obj, descs[i][0], descs[i][1]);

  return obj;
}

Browser compatibility

Based on Kangax's compat tables.

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support {{ CompatGeckoDesktop("2") }} 5 (previous versions untested) 9 11.60 5
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatGeckoMobile("2") }} {{ CompatVersionUnknown() }} {{ CompatUnknown() }} 11.50 {{ CompatVersionUnknown() }}

See also

Revision Source

<p>{{ js_minversion_header("1.8.5") }}</p>
<h3 name="Summary">Summary</h3>
<p>Defines new or modifies existing properties directly on an object, returning the object.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.8.5</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<p><code>Object.defineProperties(<em>obj</em>, <em>props</em>)</code></p>
<h3 name="Parameters">Parameters</h3>
<dl> <dt>obj</dt> <dd>The object on which to define or modify properties.</dd> <dt>props</dt> <dd>An object whose own enumerable properties constitute descriptors for the properties to be defined or modified.</dd>
</dl>
<h3 name="Description">Description</h3>
<p><code>Object.defineProperties</code>, in essence, defines all properties corresponding to the enumerable own properties of <code>props</code> on the object <code>objrops</code> object.</p>
<h3>Example usage</h3>
<pre class="brush: js">Object.defineProperties(obj, {
  "property1": {
    value: true,
    writable: true
  },
  "property2": {
    value: "Hello",
    writable: false
  }
  // etc. etc.
});
</pre>
<h3>Polyfill</h3>
<p>Assuming a pristine execution environment with all names and properties referring to their initial values, <code>Object.defineProperties</code> is almost completely equivalent (note the comment in <code>isCallable</code>) to the following reimplementation in JavaScript:</p>
<pre class="brush: js">function defineProperties(obj, properties)
{
  function convertToDescriptor(desc)
  {
    function hasProperty(obj, prop)
    {
      return Object.prototype.hasOwnProperty.call(obj, prop);
    }

    function isCallable(v)
    {
      <strong>// NB: modify as necessary if other values than functions are callable.</strong>
      return typeof v === "function";
    }

    if (typeof desc !== "object" || desc === null)
      throw new TypeError("bad desc");

    var d = {};
    if (hasProperty(desc, "enumerable"))
      d.enumerable = !!obj.enumerable;
    if (hasProperty(desc, "configurable"))
      d.configurable = !!obj.configurable;
    if (hasProperty(desc, "value"))
      d.value = obj.value;
    if (hasProperty(desc, "writable"))
      d.writable = !!desc.writable;
    if (hasProperty(desc, "get"))
    {
      var g = desc.get;
      if (!isCallable(g) &amp;&amp; g !== "undefined")
        throw new TypeError("bad get");
      d.get = g;
    }
    if (hasProperty(desc, "set"))
    {
      var s = desc.set;
      if (!isCallable(s) &amp;&amp; s !== "undefined")
        throw new TypeError("bad set");
      d.set = s;
    }

    if (("get" in d || "set" in d) &amp;&amp; ("value" in d || "writable" in d))
      throw new TypeError("identity-confused descriptor");

    return d;
  }

  if (typeof obj !== "object" || obj === null)
    throw new TypeError("bad obj");

  properties = Object(properties);
  var keys = Object.keys(properties);
  var descs = [];
  for (var i = 0; i &lt; keys.length; i++)
    descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);
  for (var i = 0; i &lt; descs.length; i++)
    Object.defineProperty(obj, descs[i][0], descs[i][1]);

  return obj;
}
</pre>
<h2>Browser compatibility</h2>
<p>Based on <a class="external" href="http://kangax.github.com/es5-compat-table/">Kangax's compat tables</a>.</p>
<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>{{ CompatGeckoDesktop("2") }}</td> <td>5 (previous versions untested)</td> <td>9</td> <td>11.60</td> <td>5</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>{{ CompatGeckoMobile("2") }}</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>11.50</td> <td>{{ CompatVersionUnknown() }}</td> </tr> </tbody> </table>
</div>
<h2 name="See_also">See also</h2>
<ul> <li><code><a href="/en/JavaScript/Reference/Global_Objects/Object/defineProperty" title="en/Core JavaScript 1.5 Reference/Global Objects/Object/defineProperty">Object.defineProperty</a></code></li> <li><code><a href="/en/JavaScript/Reference/Global_Objects/Object/keys" title="en/Core JavaScript 1.5 Reference/Global Objects/Object/keys">Object.keys</a></code></li>
</ul>
Revert to this revision