Further minor changes to punctuation and wording

(even more minor)
This commit is contained in:
Peter Roche 2020-11-24 02:22:47 -07:00 committed by GitHub
parent dcb3758f54
commit 865c3290ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -39,11 +39,11 @@ The object is stored somewhere in memory (at the right of the picture), while th
We may think of an object variable, such as `user`, as like a sheet of paper with the address.
When we perform actions with the object, e.g. take a property `user.name`, JavaScript engine looks at what's at that address and performs the operation on the actual object.
When we perform actions with the object, e.g. take a property `user.name`, the JavaScript engine looks at what's at that address and performs the operation on the actual object.
Now here's why it's important.
**When an object variable is copied, the reference is copied but the object is not duplicated.**
**When an object variable is copied, the reference is copied, but the object itself is not duplicated.**
For instance:
@ -53,13 +53,13 @@ let user = { name: "John" };
let admin = user; // copy the reference
```
Now we have two variables, each one with the reference to the same object:
Now we have two variables, each storing a reference to the same object:
![](variable-copy-reference.svg)
As you can see, there's still one object, but now with two variables that reference it.
We can use any variable to access the object and modify its contents:
We can use either variable to access the object and modify its contents:
```js run
let user = { name: 'John' };
@ -73,7 +73,7 @@ admin.name = 'Pete'; // changed by the "admin" reference
alert(*!*user.name*/!*); // 'Pete', changes are seen from the "user" reference
```
It's as if we had a cabinet with two keys and used one of them (`admin`) to get into it and make changes. Then, if we later use another key (`user`), we are still opening the same cabinet and can see the changed content.
It's as if we had a cabinet with two keys and used one of them (`admin`) to get into it and make changes. Then, if we later use another key (`user`), we are still opening the same cabinet and can access the changed contents.
## Comparison by reference
@ -106,7 +106,7 @@ So, copying an object variable creates one more reference to the same object.
But what if we need to duplicate an object? Create an independent copy, a clone?
That's also doable, but a little bit more difficult, because there's no built-in method for that in JavaScript. Actually, that's rarely needed. Copying by reference is good most of the time.
That's also doable, but a little bit more difficult, because there's no built-in method for that in JavaScript. But there is rarely a need -- copying by reference is good most of the time.
But if we really want that, then we need to create a new object and replicate the structure of the existing one by iterating over its properties and copying them on the primitive level.
@ -225,7 +225,7 @@ user.sizes.width++; // change a property from one place
alert(clone.sizes.width); // 51, see the result from the other one
```
To fix that, we should use the cloning loop that examines each value of `user[key]` and, if it's an object, then replicate its structure as well. That is called a "deep cloning".
To fix that, we should use a cloning loop that examines each value of `user[key]` and, if it's an object, then replicate its structure as well. That is called a "deep cloning".
We can use recursion to implement it. Or, to not reinvent the wheel, take an existing implementation, for instance [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep) from the JavaScript library [lodash](https://lodash.com).
@ -248,7 +248,7 @@ alert(user.name); // Pete
It might seem that the line `(*)` would cause an error, but it does not. The value of `user` is constant, it must always reference the same object. But properties of that object are free to change.
In other words, the `const user` gives an error only if we try to set `user=...` as a whole, and that's all.
In other words, the `const user` gives an error only if we try to set `user=...` as a whole.
That said, if we really need to make constant object properties, it's also possible, but using totally different methods. We'll mention that in the chapter <info:property-descriptors>.
````