JS: Object.defineProperty

By Xah Lee. Date: . Last updated: .

Object.defineProperty(obj, key, descriptor)

Create a property with specified attribute values (if key doesn't exist), or modify a existing property's attributes.

Return the modified object obj.

descriptor is a syntax for property value and attributes.

[see JS: Property Descriptor]

[see JS: Property Attributes, writable, enumerable, configurable]

// Object.defineProperty example

const x = {};

// define a property and assign its value and attributes
Object.defineProperty(x, "p",
{ value : 3,
writable: true,
enumerable: false,
configurable: true}
);

console.log(x["p"] === 3); // true

Make a property read-only:

// example of setting property to be read-only

// "use strict"

const ob = {"p":3};

Object.defineProperty(ob, "p", { writable: false} );

ob["p"] = 4; // no effect

// if in use strict mode, then it's
// TypeError: Cannot assign to read only property 'p' of #<Object>

console.log( ob["p"] === 3 ); // true

Note: once a property's “writable” attribute is set false, it can't be set back to true.

ECMAScript® 2016 Language Specification#sec-invariants-of-the-essential-internal-methods

Reflect.defineProperty

See also: JS: Reflect.defineProperty

Default Values for Property Descriptor

see JS: Property Descriptor

Define Getter Property

Object.defineProperty(object_name, key, { get : function_with_0_param, enumerable: false, configurable: true});

const ob = {p1:3};

// add a getter property key "g"
Object.defineProperty(
    ob,
    "g",
    { get : function () { console.log("getter called ") }}
);

ob.g;
// prints: getter called

[see JS: Getter/Setter Properties]

Define Setter Property

Object.defineProperty(object_name, key, { set : function_with_1_param, enumerable: false, configurable: true});

const ob = {p1:3};

// add a setter property key "s"
Object.defineProperty(
    ob,
    "s",
    { set : function (x) { console.log("setter called with arg " +x) }}
);

ob.s = 4;
// prints: setter called with arg 4

[see JS: Getter/Setter Properties]

Define Multiple Properties

[see JS: Object.defineProperties]

Reference

ECMAScript® 2016 Language Specification#sec-object.defineproperty

JS Object Property

  1. Property Overview
  2. Property Key
  3. Dot vs Bracket Notation
  4. Create/Delete Property
  5. Get/Set Property
  6. Check Property Existence
  7. Access Property
  8. List Properties
  9. Property Attributes
  10. Getter/Setter
  11. Property Descriptor
  12. Symbol

JS Object and Inheritance

  1. Object Overview
  2. Object Type
  3. Prototype and Inheritance
  4. Create Object
  5. Object Literal Expression
  6. Create Object with Parent X
  7. Get/Set Prototype
  8. Prevent Adding Property
  9. Determine Type of Object
  10. Clone Object
  11. Test Object Equality
  12. Add Method to Prototype
  13. Object Object
  14. Object.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog