Object.getOwnPropertyDescriptors()

Introduced in ES8, the Object.getOwnPropertyDescriptors() method will return all the property descriptors for a given object. What does that mean exactly? Let's take a look:

const details = {   get food1() { return 'tasty'; },  get food2() { return 'bad'; }};Object.getOwnPropertyDescriptors(details);

The output produced is:

{    food1: {        configurable: true,        enumerable: true,        get: function food1(){}, //the getter function        set: undefined    },    food2: {        configurable: true,        enumerable: true,        get: function food2(){}, //the getter function        set: undefined    }}
The get() function fires when you try to access the property (but when you also want to do a bunch of stuff first). So, when you do details.food1, tasty is returned. ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.