Object.getOwnPropertyDescriptor()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
  • Revision title: getOwnPropertyDescriptor
  • Revision id: 296224
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{ js_minversion_header("1.8.5") }}

Returns a property descriptor for an own property (that is, one directly present on an object, not present by dint of being along an object's prototype chain) of a given object.

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

Syntax

Object.getOwnPropertyDescriptor(obj, prop)

Parameters

obj
The object in which to look for the property.
prop
The name of the property whose description is to be retrieved

Description

This method permits examination of the precise description of a property. A property in JavaScript consists of a string-valued name and a property descriptor. A property descriptor is a record with some of the following attributes:

value
The value associated with the property (data descriptors only).
writable
true if and only if the value associated with the property may be changed (data descriptors only).
get
A function which serves as a getter for the property, or undefined if there is no getter (accessor descriptors only).
set
A function which serves as a setter for the property, or undefined if there is no setter (accessor descriptors only).
configurable
true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object.

Further information about property descriptor attributes can be found on the Object.defineProperty description

Examples

var o, d;

o = { get foo() { return 17; } };
d = Object.getOwnPropertyDescriptor(o, "foo");
// d is { configurable: true, enumerable: true, get: /*the getter function*/, set: undefined }

o = { bar: 42 };
d = Object.getOwnPropertyDescriptor(o, "bar");
// d is { configurable: true, enumerable: true, value: 42, writable: true }

o = {};
Object.defineProperty(o, "baz", { value: 8675309, writable: false, enumerable: false });
d = Object.getOwnPropertyDescriptor(o, "baz");
// d is { value: 8675309, writable: false, enumerable: false, configurable: false }

Cross-browser compatibility

Based on Kangax's compat table.

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 4 (2.0) 5 8 12 alpha 5
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

See also

{{ languages( {"zh-cn": "zh-cn/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor" } ) }}

Revision Source

<p>{{ js_minversion_header("1.8.5") }}</p>
<p>Returns a property descriptor for an <dfn>own property</dfn> (that is, one directly present on an object, not present by dint of being along an object's prototype chain) of a given object.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Math" 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>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<p><code>Object.getOwnPropertyDescriptor(<em>obj</em>, <em>prop</em>)</code></p>
<h2 id="Parameters" name="Parameters">Parameters</h2>
<dl>
  <dt>
    <code>obj</code></dt>
  <dd>
    The object in which to look for the property.</dd>
  <dt>
    <code>prop</code></dt>
  <dd>
    The name of the property whose description is to be retrieved</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>This method permits examination of the precise description of a property. A <dfn>property</dfn> in JavaScript consists of a string-valued name and a property descriptor. A <dfn>property descriptor</dfn> is a record with some of the following attributes:</p>
<dl>
  <dt>
    <code><strong>value</strong></code></dt>
  <dd>
    The value associated with the property (data descriptors only).</dd>
  <dt>
    <code><strong>writable</strong></code></dt>
  <dd>
    <code>true</code> if and only if the value associated with the property may be changed (data descriptors only).</dd>
  <dt>
    <code><strong>get</strong></code></dt>
  <dd>
    A function which serves as a getter for the property, or <code>undefined</code> if there is no getter (accessor descriptors only).</dd>
  <dt>
    <code><strong>set</strong></code></dt>
  <dd>
    A function which serves as a setter for the property, or <code>undefined</code> if there is no setter (accessor descriptors only).</dd>
  <dt>
    <code><strong>configurable</strong></code></dt>
  <dd>
    <code>true</code> if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.</dd>
  <dt>
    <code><strong>enumerable</strong></code></dt>
  <dd>
    <code>true</code> if and only if this property shows up during enumeration of the properties on the corresponding object.</dd>
</dl>
<p>Further information about property descriptor attributes can be found on the <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> description</p>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: js">
var o, d;

o = { get foo() { return 17; } };
d = Object.getOwnPropertyDescriptor(o, "foo");
// d is { configurable: true, enumerable: true, get: /*the getter function*/, set: undefined }

o = { bar: 42 };
d = Object.getOwnPropertyDescriptor(o, "bar");
// d is { configurable: true, enumerable: true, value: 42, writable: true }

o = {};
Object.defineProperty(o, "baz", { value: 8675309, writable: false, enumerable: false });
d = Object.getOwnPropertyDescriptor(o, "baz");
// d is { value: 8675309, writable: false, enumerable: false, configurable: false }</pre>
<h2 id="Cross-browser_compatibility">Cross-browser compatibility</h2>
<p>Based on <a class="external" href="http://kangax.github.com/es5-compat-table/">Kangax's compat table</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>4 (2.0)</td>
        <td>5</td>
        <td>8</td>
        <td>12 alpha</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>{{ 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><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>
</ul>
<p>{{ languages( {"zh-cn": "zh-cn/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor" } ) }}</p>
Revert to this revision