Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is Object.defineProperties() in Javascript?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Object.defineProperties() method defines new properties to an object or modifies the existing properties of an object.

The defineProperties() method is declared as follows:

Object.defineProperties( obj, prop)
  • obj: The object whose properties are defined.
  • prop: An object whose keys represent the name of the properties to be set and whose values represent the descriptors of the properties to be set.

Property descriptor

The property descriptor can be used to:

  • Change the value of the property, as shown below:
Object.defineProperties( obj, {prop: {value: "new Value"} } )
  • Change the meta-data of the property, as shown below:
Object.defineProperties( obj, {prop: {writable: bool} } )

Object.defineProperties( obj, {prop: {enumerable: bool} } )

Object.defineProperties( obj, {prop: {configureable: bool} } )

Where bool is either true or false.

Return value

The defineProperties() method returns the object obj with the properties prop set.

Browser compatibility

The defineProperties() method is supported by the following browsers:

  • Edge 12
  • Firefox 4
  • Google Chrome 5
  • Internet Explorer 9
  • Opera 11.6
  • Safari 5

Code

Consider the code snippet below, which updates the existing properties using the defineProperties() method:

var Student = { name: 'Ali', rollNo:'12' }
console.log(Student)
Object.defineProperties(Student,
{
name: {value: 'Ali Ahmad', writable: false},
rollNo: {value: '123'},
})
console.log(Student)

RELATED TAGS

javascript

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring