Skip to content

Commit 131c391

Browse files
authored
Merge branch 'master' into sync-d6e88647
2 parents d0a285f + f376923 commit 131c391

File tree

22 files changed

+114
-630
lines changed

22 files changed

+114
-630
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# Object references and copying
23

34
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".
@@ -8,6 +9,7 @@ Let's start with a primitive, such as a string.
89

910
Here we put a copy of `message` into `phrase`:
1011

12+
1113
```js
1214
let message = "Hello!";
1315
let phrase = message;
@@ -25,12 +27,14 @@ Objects are not like that.
2527

2628
Let's look at an example of such variable:
2729

30+
2831
```js
2932
let user = {
3033
name: "John"
3134
};
3235
```
3336

37+
3438
And here's how it's actually stored in memory:
3539

3640
![](variable-contains-reference.svg)
@@ -50,6 +54,7 @@ For instance:
5054
```js no-beautify
5155
let user = { name: "John" };
5256

57+
5358
let admin = user; // copy the reference
5459
```
5560

@@ -61,6 +66,7 @@ As you can see, there's still one object, now with two variables that reference
6166

6267
We can use any variable to access the object and modify its contents:
6368

69+
6470
```js run
6571
let user = { name: 'John' };
6672

@@ -96,9 +102,11 @@ And here two independent objects are not equal, even though they look alike (bot
96102
let a = {};
97103
let b = {}; // two independent objects
98104

105+
99106
alert( a == b ); // false
100107
```
101108
109+
102110
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.
103111
104112
## Cloning and merging, Object.assign
@@ -120,6 +128,7 @@ let user = {
120128
};
121129

122130
*!*
131+
123132
let clone = {}; // the new empty object
124133

125134
// let's copy all user properties into it
@@ -128,6 +137,7 @@ for (let key in user) {
128137
}
129138
*/!*
130139

140+
131141
// now clone is a fully independent object with the same content
132142
clone.name = "Pete"; // changed the data in it
133143

@@ -142,6 +152,7 @@ The syntax is:
142152
Object.assign(dest, [src1, src2, src3...])
143153
```
144154
155+
145156
- The first argument `dest` is a target object.
146157
- Further arguments `src1, ..., srcN` (can be as many as needed) are source objects.
147158
- It copies the properties of all source objects `src1, ..., srcN` into the target `dest`. In other words, properties of all arguments starting from the second are copied into the first object.
@@ -174,6 +185,7 @@ alert(user.name); // now user = { name: "Pete" }
174185
175186
We also can use `Object.assign` to replace `for..in` loop for simple cloning:
176187
188+
177189
```js
178190
let user = {
179191
name: "John",
@@ -192,6 +204,7 @@ It copies all properties of `user` into the empty object and returns it.
192204
Until now we assumed that all properties of `user` are primitive. But properties can be references to other objects. What to do with them?
193205
194206
Like this:
207+
195208
```js run
196209
let user = {
197210
name: "John",
@@ -208,6 +221,7 @@ Now it's not enough to copy `clone.sizes = user.sizes`, because the `user.sizes`
208221
209222
Like this:
210223
224+
211225
```js run
212226
let user = {
213227
name: "John",
Loading
Loading

0 commit comments

Comments
 (0)