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 }.

🛑 warning: if arg is not an object, it is first converted to an object.

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

console.log(Object.getOwnPropertyDescriptors(jj));
/*
{
  dog: { 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 an object, it is first converted to an object type
console.log(
 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);