JavaScript Reference ยท Object
Object.getOwnPropertyDescriptor()
Reference for JavaScript Object.getOwnPropertyDescriptor(): read a property's value, writable, enumerable, configurable, get and set attributes, with examples.
Object.getOwnPropertyDescriptor() returns an object that describes one property of an object: its value (or getter and setter) and its three attributes writable, enumerable and configurable. It looks only at the object's own properties, not at the prototype chain.
Use it to inspect how a property is defined, to check whether it can be changed, or to copy a property exactly, including accessors, with Object.defineProperty().
Object.getOwnPropertyDescriptor(obj, "name"); // { value: "Ada", writable: true, enumerable: true, configurable: true }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Object.getOwnPropertyDescriptor(obj, prop)
| Name | Description |
|---|---|
obj |
The object to inspect. Primitives are converted to objects. |
prop |
The property key: a string or a symbol. |
Return value: a descriptor object, or undefined if the object has no own property with that key.
A data property gives value, writable, enumerable and configurable. An accessor property gives get, set, enumerable and configurable.
Exceptions: a TypeError if obj is null or undefined.
Usage notes
- Own properties only.
"toString"on a plain object returnsundefined, even thoughobj.toStringworks, because the method lives on the prototype. - The result is a fresh copy. Changing the returned descriptor does not change the property. Pass it to
Object.defineProperty()for that. - Accessors report functions, not values. Reading the descriptor of a getter does not call the getter.
undefinedas a value is not the same as missing. A property that exists with the valueundefinedreturns a descriptor; a missing property returnsundefineditself.- All at once:
Object.getOwnPropertyDescriptors()returns descriptors for every own property, which is the basis of exact object cloning with prototypes. - Built-ins have their own settings: array
lengthis writable but not enumerable or configurable, andMath.PIis not writable.
Browser support
Object.getOwnPropertyDescriptor() is available in every browser and JavaScript runtime.