Object method defineProperties() in Javascript
A defineProperties() method used to defines new or modifies an existing properties in the specified object. It returns an object after adapting the specified change.
Syntax
 1 Object.defineProperties(<object>, <descriptors>)
object : An object to define new or modify an existing properties
descriptors : An objects that includes a new or an existing descriptor object to define properties in the specified object
defineProperties() method
 1 // define object
 2 const emp = { id: 1, name: 'JavaScript', salary: 1000 };
 3 
 4 Object.defineProperties(emp, 
 5     { // adding multiple properties
 6         address: {
 7             value: 'Address line 1',
 8             writable: true,
 9             enumerable: true,
 10           },
 11         city: {}
 12     });
 13 
 14 console.log("Address :", emp.address); // prints - Address line 1
 15 console.log("City :", emp.city); // prints - undefined
 16 console.log(emp);
In the above example, we have define emp object with properties and value. A defineProperties() method called by specifying new object which adds two new properties address and city with specified values.
A newly added properties printed using console log method which prints respective values. A properties define without value considered as undefined. A resulting object properties and object is print.
Output
 1 Address : Address line 1
 2 City : undefined
 3 { id: 1, name: 'JavaScript', salary: 1000, address: 'Address line 1' }
Note : If an object includes property with value as undefined, it will be excluded while printing object.

Object property descriptor

Object property descriptor
 1 const emp = {id: 1, name: 'JavaScript' }
 2 
 3 const descriptor = {
 4   value: 1000,
 5   writable: true,
 6   enumerable: true,
 7   configurable: true
 8 };
 9 
 10 Object.defineProperties(emp, { salary: descriptor});
 11 
 12 console.log(emp);
In the above example, a emp object is created with two properties and values. A JavaScript Object descriptor created that with four properties, value specifies a value of property, writable specifies whether property modification allowed, enumerable specifies whether a property allows to print, or iterable using for...in loop and configurable specifies whether a property is allows to configure.
Output
 1 { id: 1, name: 'JavaScript', salary: 1000 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us