The Object.getOwnPropertyDescriptor()
method returns a property descriptor for an own property (that is, one directly present on an object and not in the object's prototype chain) of a given object.
Object.getOwnPropertyDescriptor(obj, prop)
obj
prop
A property descriptor of the given property if it exists on the object, undefined
otherwise.
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 Object.defineProperty()
.
A property descriptor is a record with some of the following attributes:
value
writable
true
if and only if the value associated with the property may be changed (data descriptors only).get
undefined
if there is no getter (accessor descriptors only).set
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.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 }
In ES5, if the first argument to this method is not an object (a primitive), then it will cause a TypeError
. In ES2015, a non-object first argument will be coerced to an object at first.
Object.getOwnPropertyDescriptor('foo', 0); // TypeError: "foo" is not an object // ES5 code Object.getOwnPropertyDescriptor('foo', 0); // {configurable:false, enumerable:true, value:"f", writable:false} // ES2015 code
Specification | Status | Comment |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Object.getOwnPropertyDescriptor' in that specification. | Standard | Initial definition. Implemented in JavaScript 1.8.5. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.getOwnPropertyDescriptor' in that specification. | Standard | |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Object.getOwnPropertyDescriptor' in that specification. | Draft |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5 | 4.0 (2) | 8 | 12 | 5 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | ? | ? | ? | ? |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor