This chapter explains the ECMAScript 2017 feature “
Object.getOwnPropertyDescriptors()” by Jordan Harband and Andrea Giammarchi.
Object.getOwnPropertyDescriptors(obj) returns the property descriptors of all own properties of
obj, in an Array:
Object.getOwnPropertyDescriptors(obj) accepts an object
obj and returns an object
obj, it adds a property to
resultwhose key is the same and whose value is the the former property’s descriptor.
This is an example of using
This is how you would implement
Object.assign(). However, this method uses simple get and set operations to copy a property whose key is
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
Object.assign() to copy property
foo to object
Object.getOwnPropertyDescriptors() together with
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 syntactically nicest way of using an object literal to create an object with an arbitrary prototype
prot is to use the special property
Alas, that feature is only guaranteed to be there in browsers. The common work-around is
Object.create() and assignment:
But you can also use
Another alternative is
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.