minor
This commit is contained in:
parent
d1cf45eda8
commit
d32eabef0f
2 changed files with 6 additions and 4 deletions
|
@ -271,7 +271,7 @@ So, we can set many properties at once.
|
||||||
|
|
||||||
## Object.getOwnPropertyDescriptors
|
## Object.getOwnPropertyDescriptors
|
||||||
|
|
||||||
To get many descriptors at once, we can use the method [Object.getOwnPropertyDescriptors(obj)](mdn:js/Object/getOwnPropertyDescriptors).
|
To get all property descriptors at once, we can use the method [Object.getOwnPropertyDescriptors(obj)](mdn:js/Object/getOwnPropertyDescriptors).
|
||||||
|
|
||||||
Together with `Object.defineProperties` it can be used as a "flags-aware" way of cloning an object:
|
Together with `Object.defineProperties` it can be used as a "flags-aware" way of cloning an object:
|
||||||
|
|
||||||
|
@ -289,6 +289,8 @@ for(let key in user) {
|
||||||
|
|
||||||
...But that does not copy flags. So if we want a "better" clone then `Object.defineProperties` is preferred.
|
...But that does not copy flags. So if we want a "better" clone then `Object.defineProperties` is preferred.
|
||||||
|
|
||||||
|
Another difference is that `for..in` ignores symbolic properties, but `Object.getOwnPropertyDescriptors` returns *all* property descriptors including symbolic ones.
|
||||||
|
|
||||||
## Sealing an object globally
|
## Sealing an object globally
|
||||||
|
|
||||||
Property descriptors work at the level of individual properties.
|
Property descriptors work at the level of individual properties.
|
||||||
|
|
|
@ -51,14 +51,14 @@ alert(rabbit.jumps); // true
|
||||||
|
|
||||||
The descriptors are in the same format as described in the chapter <info:property-descriptors>.
|
The descriptors are in the same format as described in the chapter <info:property-descriptors>.
|
||||||
|
|
||||||
We can use `Object.create` to perform a full object cloning, like this:
|
We can use `Object.create` to perform an object cloning more powerful than copying properties in `for..in`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// fully identical shallow clone of obj
|
// fully identical shallow clone of obj
|
||||||
let clone = Object.create(obj, Object.getOwnPropertyDescriptors(obj));
|
let clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
|
||||||
```
|
```
|
||||||
|
|
||||||
This call makes a truly exact copy of `obj`, including all properties: enumerable and non-enumerable, data properties and setters/getters -- everything, and with the right `[[Prototype]]`. But not an in-depth copy of course.
|
This call makes a truly exact copy of `obj`, including all properties: enumerable and non-enumerable, data properties and setters/getters -- everything, and with the right `[[Prototype]]`.
|
||||||
|
|
||||||
## Brief history
|
## Brief history
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue