Object.defineProperty
Object.defineProperty is a JavaScript built-in method that creates or modifies an object property using an explicitly specified property descriptor.
Secrets of the JavaScript Ninja, Second Edition
A JavaScript method that defines or modifies a property on an object with explicit control over its attributes.
- How it is used
- Configures property attributes for enumeration and prototype repair, or defines accessors that respond to property reads and writes.
- How deep it goes
- Works through descriptor attributes, a constructor-property repair, and a getter/setter implementation rather than merely naming the API.
- Examples
- Uses a nonenumerable `sneaky` property, a restored prototype `constructor`, and a `skillLevel` property backed by getter and setter functions.
- What you can do after
- Apply it via Object.defineProperty to control enumeration, restore a prototype’s constructor property, or define getter and setter behavior.
Read how Secrets of the JavaScript Ninja, Second Edition explains Object.defineProperty →
Secrets of the JavaScript Ninja, Third Edition
Object.defineProperty is a JavaScript built-in method that creates or modifies an object's property with a specified property descriptor.
- How it is used
- Adds properties dynamically or controls their behavior through descriptors, especially when defining accessors on existing objects.
- How deep it goes
- Explains accessor and data-property descriptors through examples of enumeration, assignment, and deletion behavior.
- Examples
- Uses a ninjaCollection accessor, a random-number getter, a password-hashing setter, property deletion, and a value property.
- What you can do after
- Apply it to add accessors or data properties and control their enumeration, writability, or configurability.
Read how Secrets of the JavaScript Ninja, Third Edition explains Object.defineProperty →
JavaScript in Depth
A JavaScript standard library method that defines or redefines a property with explicit descriptor attributes.
- How it is used
- Controls property mutability, enumeration, deletion, redefinition, and accessor behavior through explicit descriptors.
- How deep it goes
- Explains descriptor flags, default differences, strictness effects, and accessor descriptors, stopping short of broader runtime internals.
- Examples
- Uses drawer contents, internal identifiers, serial numbers, object-literal defaults, and getter-setter properties.
- What you can do after
- Apply it via Object.defineProperty() to create controlled data or accessor properties and predict assignment, iteration, and deletion outcomes.
Read how JavaScript in Depth explains Object.defineProperty →
Also covered in
What to know first
- object literalAn object literal is a language expression that directly specifies an object value and its constituent properties or values.
- attributeAn attribute is a named property or piece of information associated with an entity, object, or structured data item.
- classA class is a programming-language construct that defines a type’s structure and behavior and provides a basis for creating or representing objects.
Related concepts
- constructor propertyproduces · The constructor property is an object property that refers to the constructor function associated with the object or its prototype.
- property descriptoruses · A property descriptor is an object specifying a JavaScript property's data or accessor attributes and behavior.
- getteruses · A property accessor that executes code and returns a value when the property is read.
- accessor descriptoruses · An accessor descriptor defines an object property whose value is read or changed through getter and setter functions rather than directly stored.
- setteruses · A setter is an accessor function associated with an object property that is invoked when a value is assigned to that property.
- Object.assigncontrasts with · Object.assign is a JavaScript static method that copies enumerable own properties from source objects to a target object and returns the target.
Questions about Object.defineProperty
What is Object.defineProperty?
Object.defineProperty is a JavaScript built-in method that creates or modifies an object property using an explicitly specified property descriptor.
Which Manning books explain Object.defineProperty?
Secrets of the JavaScript Ninja, Second Edition, Secrets of the JavaScript Ninja, Third Edition and JavaScript in Depth.
What should I know before learning Object.defineProperty?
Manning books that teach Object.defineProperty usually introduce for...in, object literal, attribute and class first.
What is the difference between Object.defineProperty and Object.assign?
Object.defineProperty is a JavaScript built-in method that creates or modifies an object property using an explicitly specified property descriptor. Object.assign is a JavaScript static method that copies enumerable own properties from source objects to a target object and returns the target.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


