JavaScript ES5 Object Methods

ECMAScript 5 (2009) added many new object methods to JavaScript.

Manage object

// Create an object with an existing object as the prototype
Object.create()
// Add or change object properties
Object.defineProperty(object, property, descriptor)
// Add or change object properties
Object.defineProperties(object, descriptors)
// Access attribute
Object.getOwnPropertyDescriptor(object, property)
// Returns an array of all properties
Object.getOwnPropertyNames(object)
// Access prototype
Object.getPrototypeOf(object)
// Returns an array of enumerable properties
Object.keys(object)

Protect object

// Prevent adding properties to the object
Object.preventExtensions(object)
// Returns true if attributes can be added to the object
Object.isExtensible(object)
// Prevent changes to object properties (not values)
Object.seal(object)
// Returns true if the object is sealed
Object.isSealed(object)
// Prevent any changes to the object
Object.freeze(object)
// Returns true if the object is frozen
Object.isFrozen(object)

Change attribute value

Syntax

Object.defineProperty(object, property, {value : value)

Instance

This example changes the attribute value:

const person = {
  firstName: "Bill",
  lastName : "Gates",
  language : "EN"
};
// Modify attribute
Object.defineProperty(person, "language", {value : "NO"});

Try It Yourself

Modify metadata

ES5 allows the modification of the following attribute metadata:

writable : true      // Attribute value can be changed
enumerable : true    // Attribute can be enumerated
configurable : true  // Attribute can be reconfigured
writable : false     // Attribute value cannot be changed
enumerable : false   // Attribute is not enumerable
configurable : false // The property cannot be reconfigured

ES5 allows changes to getters and setters:

// Define getter
get: function() { return language }
// Define setter
set: function(value) { language = value }

This example sets language to read-only:

Object.defineProperty(person, "language", {writable:false});

This example makes language non-enumerable:

Object.defineProperty(person, "language", {enumerable:false});

List all properties

This example lists all properties of an object:

Instance

const person = {
  firstName: "Bill",
  lastName : "Gates",
  language : "EN"
};
Object.defineProperty(person, "language", {enumerable:false});
Object.getOwnPropertyNames(person);  // Returns an array of arrays

Try It Yourself

List enumerable properties

This example lists only the enumerable properties of the object:

Instance

const person = {
  firstName: "Bill",
  lastName : "Gates",
  language : "EN"
};
Object.defineProperty(person, "language", {enumerable:false});
Object.keys(person);  // Returns an array of enumerable properties

Try It Yourself

Add property

This example adds a new property to the object:

Instance

// Create object
const person = {
  firstName: "Bill",
  lastName : "Gates",
  language : "EN"
};
// Add property
Object.defineProperty(person, "year", {value:"2008"});

Try It Yourself

Add Getters and Setters

Object.defineProperty() Methods can also be used to add Getters and Setters:

Instance

// Create object
const person = {firstName:"Bill", lastName:"Gates"};
// Define getter
Object.defineProperty(person, "fullName", {
  get: function () {return this.firstName + " " + this.lastName;}
});

Try It Yourself

An instance of a counter

// Define object
const obj = {counter:0};
// Define setter
Object.defineProperty(obj, "reset", {
  get : function () {this.counter = 0;}
});
Object.defineProperty(obj, "increment", {
  get : function () {this.counter++;}
});
Object.defineProperty(obj, "decrement", {
  get : function () {this.counter--;}
});
Object.defineProperty(obj, "add", {
  set : function (value) {this.counter += value;}
});
Object.defineProperty(obj, "subtract", {
  set : function (i) {this.counter -= i;}
});
// Counter Operation:
obj.reset;
obj.add = 5;
obj.subtract = 1;
obj.increment;
obj.decrement;

Try It Yourself