Please support this book: buy it (PDF, EPUB, MOBI) or donate

9. Object.getOwnPropertyDescriptors()

This chapter explains the ECMAScript 2017 feature “Object.getOwnPropertyDescriptors()” by Jordan Harband and Andrea Giammarchi.

9.1 Overview

Object.getOwnPropertyDescriptors(obj) returns the property descriptors of all own properties of obj, in an Array:

  1. const obj = {
  2. [Symbol('foo')]: 123,
  3. get bar() { return 'abc' },
  4. };
  5. console.log(Object.getOwnPropertyDescriptors(obj));
  6.  
  7. // Output:
  8. // { [Symbol('foo')]:
  9. // { value: 123,
  10. // writable: true,
  11. // enumerable: true,
  12. // configurable: true },
  13. // bar:
  14. // { get: [Function: bar],
  15. // set: undefined,
  16. // enumerable: true,
  17. // configurable: true } }

9.2 Object.getOwnPropertyDescriptors()

Object.getOwnPropertyDescriptors(obj) accepts an object obj and returns an object result:

  • For each own (non-inherited) property of obj, it adds a property to result whose key is the same and whose value is the the former property’s descriptor.
    Property descriptors describe the attributes of a property (its value, whether it is writable, etc.). For more information, consult Sect. “Property Attributes and Property Descriptors” in “Speaking JavaScript”.

This is an example of using Object.getOwnPropertyDescriptors():

  1. const obj = {
  2. [Symbol('foo')]: 123,
  3. get bar() { return 'abc' },
  4. };
  5. console.log(Object.getOwnPropertyDescriptors(obj));
  6.  
  7. // Output:
  8. // { [Symbol('foo')]:
  9. // { value: 123,
  10. // writable: true,
  11. // enumerable: true,
  12. // configurable: true },
  13. // bar:
  14. // { get: [Function: bar],
  15. // set: undefined,
  16. // enumerable: true,
  17. // configurable: true } }

This is how you would implement Object.getOwnPropertyDescriptors():

  1. function getOwnPropertyDescriptors(obj) {
  2. const result = {};
  3. for (let key of Reflect.ownKeys(obj)) {
  4. result[key] = Object.getOwnPropertyDescriptor(obj, key);
  5. }
  6. return result;
  7. }

9.3 Use cases for Object.getOwnPropertyDescriptors()

9.3.1 Use case: copying properties into an object

Since ES6, JavaScript already has a tool method for copying properties: Object.assign(). However, this method uses simple get and set operations to copy a property whose key is key:

  1. const value = source[key]; // get
  2. target[key] = value; // set

That means that it doesn’t properly copy properties with non-default attributes (getters, setters, non-writable properties, etc.). The following example illustrates this limitation. The object source has a setter whose key is foo:

  1. const source = {
  2. set foo(value) {
  3. console.log(value);
  4. }
  5. };
  6. console.log(Object.getOwnPropertyDescriptor(source, 'foo'));
  7. // { get: undefined,
  8. // set: [Function: foo],
  9. // enumerable: true,
  10. // configurable: true }

Using Object.assign() to copy property foo to object target fails:

  1. const target1 = {};
  2. Object.assign(target1, source);
  3. console.log(Object.getOwnPropertyDescriptor(target1, 'foo'));
  4. // { value: undefined,
  5. // writable: true,
  6. // enumerable: true,
  7. // configurable: true }

Fortunately, using Object.getOwnPropertyDescriptors() together with Object.defineProperties() works:

  1. const target2 = {};
  2. Object.defineProperties(target2, Object.getOwnPropertyDescriptors(source));
  3. console.log(Object.getOwnPropertyDescriptor(target2, 'foo'));
  4. // { get: undefined,
  5. // set: [Function: foo],
  6. // enumerable: true,
  7. // configurable: true }

9.3.2 Use case: cloning objects

Shallow cloning is similar to copying properties, which is why Object.getOwnPropertyDescriptors() is a good choice here, too.

This time, we use Object.create() that has two parameters:

  • The first parameter specifies the prototype of the object it returns.
  • The optional second parameter is a property descriptor collection like the ones returned by Object.getOwnPropertyDescriptors().
  1. const clone = Object.create(Object.getPrototypeOf(obj),
  2. Object.getOwnPropertyDescriptors(obj));

9.3.3 Use case: cross-platform object literals with arbitrary prototypes

The syntactically nicest way of using an object literal to create an object with an arbitrary prototype prot is to use the special property proto:

  1. const obj = {
  2. __proto__: prot,
  3. foo: 123,
  4. };

Alas, that feature is only guaranteed to be there in browsers. The common work-around is Object.create() and assignment:

  1. const obj = Object.create(prot);
  2. obj.foo = 123;

But you can also use Object.getOwnPropertyDescriptors():

  1. const obj = Object.create(
  2. prot,
  3. Object.getOwnPropertyDescriptors({
  4. foo: 123,
  5. })
  6. );

Another alternative is Object.assign():

  1. const obj = Object.assign(
  2. Object.create(prot),
  3. {
  4. foo: 123,
  5. }
  6. );

9.4 Pitfall: copying methods that use super

A method that uses super is firmly connected with its home object (the object it is stored in). There is currently no way to copy or move such a method to a different object.