Object.getOwnPropertyDescriptor()

Returns the full attribute record for one own property, or undefined if it is not there.

Signature
Object.getOwnPropertyDescriptor(obj, prop)
  • Added in JavaScript ES5

Parameters

ParameterTypeDescription
$objObjectThe object in which to look for the property.
$propstring | symbolThe name or symbol of the property whose description is to be retrieved.

Return value

Object | undefinedA property descriptor object for the given own property, or undefined if the property does not exist as an own property.

Errors

Throws a TypeError only if obj is null or undefined; other primitives are coerced to objects.

Changelog

  • PHP ES2015A non-object first argument is coerced to an object instead of throwing a TypeError, so Object.getOwnPropertyDescriptor('ab', 0) now returns a descriptor.

Object.getOwnPropertyDescriptor() is the read side of Object.defineProperty(). It reports exactly how a property is configured rather than what it evaluates to — the four attributes for a data property (value, writable, enumerable, configurable) or the four for an accessor (get, set, enumerable, configurable).

When to reach for it

  • Copying a property faithfully from one object to another. Plain assignment and spread invoke getters and flatten them into ordinary values; reading the descriptor and passing it to defineProperty() preserves the getter itself.
  • Debugging a property that will not accept a write, to see whether writable is false.
  • Finding out why a key is missing from Object.keys() — the descriptor tells you if it is simply non-enumerable.

What trips people up

It is strictly an own property lookup. Inherited properties, including everything on a class prototype, return undefined. To inspect a method defined in a class body, ask the prototype: Object.getOwnPropertyDescriptor(MyClass.prototype, 'method'). Class methods are non-enumerable, which is exactly why they never show up in a for...in over an instance.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Clone an object without collapsing its getters into static values
  • Diagnose why an assignment to a property is being ignored
  • Check whether a library marked a field as non-enumerable on purpose

Frequently asked questions

Why does getOwnPropertyDescriptor return undefined for a method that clearly exists?

Because the method is inherited rather than own. Class and prototype methods live on the prototype, so query Object.getOwnPropertyDescriptor(Class.prototype, 'name').

How do I copy an object without triggering its getters?

Read each descriptor with Object.getOwnPropertyDescriptor() (or Object.getOwnPropertyDescriptors()) and hand it to Object.defineProperty(); spread and Object.assign() call getters and store the resulting value.

How can I tell if a property is read-only?

Check the writable attribute in the returned descriptor for data properties, or check whether set is undefined for accessor properties.

More examples

Copying a getter instead of its value

Spread evaluated the getter once and stored a plain string, so the copy has no get function. Passing the descriptor through defineProperty carried the accessor across intact, and the counter proves the getter was never called during that copy.

Example · javascript
let reads = 0;
const source = {
  get now() {
    reads++;
    return "computed";
  },
};

const spread = { ...source };                 // getter already invoked
const target = {};
Object.defineProperty(
  target,
  "now",
  Object.getOwnPropertyDescriptor(source, "now"),
);

console.log(typeof Object.getOwnPropertyDescriptor(spread, "now").get); // "undefined"
console.log(typeof Object.getOwnPropertyDescriptor(target, "now").get); // "function"
console.log(reads);                            // 1 — only the spread read it

Class methods are non-enumerable

double() lives on Point.prototype, not on the instance, so the instance lookup returns undefined. The prototype descriptor shows enumerable: false, which is the reason class methods never appear in Object.keys() or a for...in loop.

Example · javascript
class Point {
  constructor(x) {
    this.x = x;
  }
  double() {
    return this.x * 2;
  }
}

const p = new Point(3);
console.log(Object.getOwnPropertyDescriptor(p, "double"));   // undefined

const d = Object.getOwnPropertyDescriptor(Point.prototype, "double");
console.log(d.enumerable, d.writable, d.configurable);      // false true true
console.log(Object.keys(p));                                // [ 'x' ]

Discussion

  • Be the first to comment on this lesson.