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

Default Values for Property Descriptor

Property Descriptor default values for all attributes is false, and for value attribute it defaults to undefined.

[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 Topic

  1. JS: Property Overview
  2. JS: Property Key
  3. JS: Property Dot Notation / Bracket Notation
  4. JS: Create/Delete Property
  5. JS: Get Property, Set Property
  6. JS: Check Property Existence
  7. JS: Access Property
  8. JS: Property Attributes, writable, enumerable, configurable
  9. JS: Getter/Setter Properties
  10. JS: Property Descriptor

Object and Inheritance Topic

  1. JS: Object Overview
  2. JS: Object Type
  3. JS: Determine Type of Object
  4. JS: Object Literal Expression
  5. JS: Prototype and Inheritance
  6. JS: Create Object
  7. JS: Create Object with Parent X
  8. JS: Get/Set Prototype
  9. JS: Prevent Adding Property
  10. JS: Clone, Deep Copy Object/Array
  11. JS: Test Equality of Objects
  12. JS: Object Object
  13. JS: 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.