JS: Object.getOwnPropertyDescriptors

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2017)

Object.getOwnPropertyDescriptors(obj)

Return Property Descriptor of all own properties of a object.

The return value is a object, has this form

{ k1: descriptor1, k2: descriptor2, etc }.

If obj is not a object, it is first converted to a object type.

// object with a string key and symbol key
const jj = {
 "p": 3,
 [Symbol()]: 4,
};

console.log(Object.getOwnPropertyDescriptors(jj));

/*
{
  p: { value: 3, writable: true, enumerable: true, configurable: true },
  [Symbol()]: { value: 4, writable: true, enumerable: true, configurable: true }
}
*/

Edge cases

Arg is not object

// If obj is not a object, it is first converted to a object type
console.log(Object.getOwnPropertyDescriptors(3));
// {}

console.assert(typeof Object.getOwnPropertyDescriptors(3));

Object sans properties

// object without properties
console.log(Object.getOwnPropertyDescriptors({}));
// {}

// result is empty object
console.assert(Object.keys(Object.getOwnPropertyDescriptors({})).length === 0);

JavaScript. Define Properties and attributes.