liveBook · Manning

Object.getOwnPropertyDescriptor

As explained in JavaScript in Depth

A JavaScript standard library method that returns the property descriptor for a specified own property.

When a property behaves unexpectedly, inspect its descriptor: Object.getOwnPropertyDescriptor() reveals whether an own property is a data property or accessor and exposes its controlling attributes. section 5.2.3

For data properties, the returned descriptor makes these controls explicit:

  • writable determines whether assignment changes the value; strict mode throws for a non-writable property, while sloppy mode silently fails. section 5.2.3
  • enumerable determines whether the property appears in for...in loops and Object.keys(); direct access still works when it is false. section 5.2.3
  • configurable determines whether deletion and later descriptor changes are allowed; writable and configurable control different operations. section 5.2.3

Data versus accessor properties

For an accessor such as drawer.contents, the descriptor exposes get and set functions rather than a value; enumerable and configurable remain available as descriptor attributes.

How JavaScript in Depth covers it

How deep it goes
Explains data and accessor descriptors, three behavior attributes, and strict-versus-sloppy assignment, but stops short of engine internals.
Examples
Uses drawer properties, non-writable identifiers, hidden keys, locked serial numbers, and getter-setter accessors.
What you can do after
Diagnose property behavior via Object.getOwnPropertyDescriptor and relate descriptor flags to assignment, iteration, deletion, and redefinition.
How it connects
Connects property descriptors with Object.defineProperty, Object.keys, for...in, data properties, and accessor properties.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.