Skip to content

Commit edef0b4

Browse files
committed
minor fixes
1 parent bdb3def commit edef0b4

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

1-js/04-object-basics/02-object-copy/article.md

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Object copying, references
1+
# Object references and copying
22

3-
One of the fundamental differences of objects vs primitives is that they are stored and copied "by reference".
3+
One of the fundamental differences of objects versus primitives is that objects are stored and copied "by reference", as opposed to primitive values: strings, numbers, booleans, etc -- that are always copied "as a whole value".
44

5-
Primitive values: strings, numbers, booleans -- are assigned/copied "as a whole value".
5+
That's easy to understand if we look a bit "under a cover" of what happens when we copy a value.
66

7-
For instance:
7+
Let's start with a primitive, such as a string.
8+
9+
Here we put a copy of `message` into `phrase`:
810

911
```js
1012
let message = "Hello!";
@@ -15,21 +17,31 @@ As a result we have two independent variables, each one is storing the string `"
1517

1618
![](variable-copy-value.svg)
1719

20+
Quite an obvious result, right?
21+
1822
Objects are not like that.
1923

20-
**A variable stores not the object itself, but its "address in memory", in other words "a reference" to it.**
24+
**A variable assigned to an object stores not the object itself, but its "address in memory", in other words "a reference" to it.**
2125

22-
Here's the picture for the object:
26+
Let's look at an example of such variable:
2327

2428
```js
2529
let user = {
2630
name: "John"
2731
};
2832
```
2933

34+
And here's how it's actually stored in memory:
35+
3036
![](variable-contains-reference.svg)
3137

32-
Here, the object is stored somewhere in memory. And the variable `user` has a "reference" to it.
38+
The object is stored somewhere in memory (at the right of the picture), while the `user` variable (at the left) has a "reference" to it.
39+
40+
We may think of an object variable, such as `user`, as of a sheet of paper with the address.
41+
42+
When we perform actions with the object, e.g. take a property `user.name`, JavaScript engine looks into that address and performs the operation on the actual object.
43+
44+
Now here's why it's important.
3345

3446
**When an object variable is copied -- the reference is copied, the object is not duplicated.**
3547

@@ -45,6 +57,8 @@ Now we have two variables, each one with the reference to the same object:
4557

4658
![](variable-copy-reference.svg)
4759

60+
As you can see, there's still one object, now with two variables that reference it.
61+
4862
We can use any variable to access the object and modify its contents:
4963

5064
```js run
@@ -59,15 +73,14 @@ admin.name = 'Pete'; // changed by the "admin" reference
5973
alert(*!*user.name*/!*); // 'Pete', changes are seen from the "user" reference
6074
```
6175
62-
The example above demonstrates that there is only one object. As if we had a cabinet with two keys and used one of them (`admin`) to get into it. Then, if we later use another key (`user`) we can see changes.
6376
64-
## Comparison by reference
77+
It's just as if we had a cabinet with two keys and used one of them (`admin`) to get into it. Then, if we later use another key (`user`) we can see changes.
6578
66-
The equality `==` and strict equality `===` operators for objects work exactly the same.
79+
## Comparison by reference
6780
68-
**Two objects are equal only if they are the same object.**
81+
Two objects are equal only if they are the same object.
6982
70-
Here two variables reference the same object, thus they are equal:
83+
For instance, here `a` and `b` reference the same object, thus they are equal:
7184
7285
```js run
7386
let a = {};
@@ -77,7 +90,7 @@ alert( a == b ); // true, both variables reference the same object
7790
alert( a === b ); // true
7891
```
7992
80-
And here two independent objects are not equal, even though both are empty:
93+
And here two independent objects are not equal, even though they look alike (both are empty):
8194
8295
```js run
8396
let a = {};
@@ -86,7 +99,7 @@ let b = {}; // two independent objects
8699
alert( a == b ); // false
87100
```
88101
89-
For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj == 5`, objects are converted to primitives. We'll study how object conversions work very soon, but to tell the truth, such comparisons occur very rarely, usually as a result of a coding mistake.
102+
For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj == 5`, objects are converted to primitives. We'll study how object conversions work very soon, but to tell the truth, such comparisons are needed very rarely, usually they appear as a result of a programming mistake.
90103
91104
## Cloning and merging, Object.assign
92105

0 commit comments

Comments
 (0)