mozilla

Revision 496193 of Object.getOwnPropertyDescriptor()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
  • Revision title: Object.getOwnPropertyDescriptor()
  • Revision id: 496193
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Object")}}

Summary

The Object.getOwnPropertyDescriptor() method 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.

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

Return value

A property descriptor of the given property if it exists on the object, undefined otherwise.

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. Further information about property descriptor types and their attributes can be found in {{jsxref("Object.defineProperty")}}.

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.

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 }

Specifications

Specification Status Comment
{{SpecName('ES5.1', '#sec-15.2.3.3', 'Object.getOwnPropertyDescriptor')}} {{Spec2('ES5.1')}} Initial definition.
Implemented in JavaScript 1.8.5
{{SpecName('ES6', '#sec-object.getownpropertydescriptor', 'Object.getOwnPropertyDescriptor')}} {{Spec2('ES6')}}  

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

  • {{jsxref("Object.defineProperty")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Object")}}</div>
<h2>Summary</h2>
<p>The <code><strong>Object.getOwnPropertyDescriptor()</strong></code> method 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>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>Object.getOwnPropertyDescriptor(<em>obj</em>, <em>prop</em>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<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>
<h3 id="Return_value">Return value</h3>
<p>A property descriptor of the given property if it exists on the object, <code>undefined</code> otherwise.</p>
<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. Further information about property descriptor types and their attributes can be found in {{jsxref("Object.defineProperty")}}.</p>
<p>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>
<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="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2.3.3', 'Object.getOwnPropertyDescriptor')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Initial definition.<br />
    Implemented in JavaScript 1.8.5</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.getownpropertydescriptor', 'Object.getOwnPropertyDescriptor')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>Based on <a class="external" href="http://kangax.github.com/es5-compat-table/">Kangax's compat table</a>.</p>
<div>
 {{CompatibilityTable}}</div>
<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>{{jsxref("Object.defineProperty")}}</code></li>
</ul>
<!--languages( {"zh-cn": "zh-cn/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor" } )-->
Revert to this revision