minor fixes

This commit is contained in:
Ilya Kantor 2021-02-02 11:43:35 +03:00
parent 19bf2d3f5a
commit 3fa4c32e1d
7 changed files with 32 additions and 23 deletions

View file

@ -186,6 +186,8 @@ let clone = Object.assign({}, user);
It copies all properties of `user` into the empty object and returns it.
There are also other methods of cloning an object, e.g. using the [spread operator](info:rest-parameters-spread) `clone = {...user}`, covered later in the tutorial.
## Nested cloning
Until now we assumed that all properties of `user` are primitive. But properties can be references to other objects. What to do with them?

View file

@ -166,9 +166,9 @@ userGuest.admin?.(); // nothing (no such method)
*/!*
```
Here, in both lines we first use the dot (`user1.admin`) to get `admin` property, because the user object must exist, so it's safe read from it.
Here, in both lines we first use the dot (`userAdmin.admin`) to get `admin` property, because we assume that the user object exists, so it's safe read from it.
Then `?.()` checks the left part: if the admin function exists, then it runs (that's so for `user1`). Otherwise (for `user2`) the evaluation stops without errors.
Then `?.()` checks the left part: if the admin function exists, then it runs (that's so for `userAdmin`). Otherwise (for `userGuest`) the evaluation stops without errors.
The `?.[]` syntax also works, if we'd like to use brackets `[]` to access properties instead of dot `.`. Similar to previous cases, it allows to safely read a property from an object that may not exist.