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]

// using Object.defineProperty

var 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

Make a property read-only:

// example of setting property to be read-only

// "use strict"

var oy = {"p":3};

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

oy["p"] = 4;                    // no effect
// if in use strict mode, then it's
// TypeError: Cannot assign to read only property 'p' of #<Object>

console.log(oy["p"]);           // 3

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});

var 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});

var 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 System Overview
  2. JS: What's Object?
  3. JS: Prototype and Inheritance
  4. JS: Create Object
  5. JS: Object Literal Expression
  6. JS: Get/Set Prototype
  7. JS: How to Create Object with Parent X?
  8. JS: Prevent Adding Property
  9. JS: Determine Type of Object
  10. JS: Primitive Value Object Wrapper
  11. JS: Clone, Deep Copy Object/Array
  12. JS: Test Equality of Objects

  1. JS: Object Object
  2. JS: Object.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon