JS: Object.defineProperties

By Xah Lee. Date: . Last updated: .
Object.defineProperties(obj, {key1: descriptor1}, {key2: descriptor2}, etc)
  • Create or modify one or more properties.
  • Return the modified obj.
  • The descriptor1, descriptor2, etc are Property Descriptor.
const yy = {};

// add 2 properties, with their attributes
Object.defineProperties(
  yy,
  {
    x1: { value: 1, writable: true, enumerable: true, configurable: true },
    x2: { value: 2, writable: true, enumerable: true, configurable: true },
  },
);

console.log(JSON.stringify(yy) === `{"x1":1,"x2":2}`);

JavaScript. Define Properties