Skip to content

Commit fc4a559

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-62299ed8
2 parents 2ebe754 + 62299ed commit fc4a559

13 files changed

Lines changed: 64 additions & 20 deletions

File tree

1-js/04-object-basics/01-object/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ user.likes birds = true
103103

104104
JavaScript doesn't understand that. It thinks that we address `user.likes`, and then gives a syntax error when comes across unexpected `birds`.
105105

106-
The dot requires the key to be a valid variable identifier. That implies: contains no spaces, doesn't start with a digit and doesn't include special characters (`$` и `_` are allowed).
106+
The dot requires the key to be a valid variable identifier. That implies: contains no spaces, doesn't start with a digit and doesn't include special characters (`$` and `_` are allowed).
107107

108108
There's an alternative "square bracket notation" that works with any string:
109109

1-js/06-advanced-functions/01-recursion/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ list.next.next.next = { value: 4 };
462462
list.next.next.next.next = null;
463463
```
464464
465-
Here we can even more clearer see that there are multiple objects, each one has the `value` and `next` pointing to the neighbour. The `list` variable is the first object in the chain, so following `next` pointers from it we can reach any element.
465+
Here we can even more clearly see that there are multiple objects, each one has the `value` and `next` pointing to the neighbour. The `list` variable is the first object in the chain, so following `next` pointers from it we can reach any element.
466466
467467
The list can be easily split into multiple parts and later joined back:
468468

1-js/06-advanced-functions/02-rest-parameters-spread/article.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,51 @@ But there's a subtle difference between `Array.from(obj)` and `[...obj]`:
225225
So, for the task of turning something into an array, `Array.from` tends to be more universal.
226226
227227
228+
## Get a new copy of an object/array
229+
230+
Remember when we talked about `Object.assign()` [in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in)?
231+
232+
It is possible to do the same thing with the spread operator!
233+
234+
```
235+
let arr = [1, 2, 3];
236+
let arrCopy = [...arr]; // spread the array into a list of parameters
237+
// then put the result into a new array
238+
239+
// do the arrays have the same contents?
240+
alert(JSON.stringify(arr) === JSON.stringify(arrCopy)); // true
241+
242+
// are the arrays equal?
243+
alert(arr === arrCopy); // false (not same reference)
244+
245+
// modifying our initial array does not modify the copy:
246+
arr.push(4);
247+
alert(arr); // 1, 2, 3, 4
248+
alert(arrCopy); // 1, 2, 3
249+
```
250+
251+
Note that it is possible to do the same thing to make a copy of an object:
252+
253+
```
254+
let obj = { a: 1, b: 2, c: 3 };
255+
let objCopy = { ...obj }; // spread the object into a list of parameters
256+
// then return the result in a new object
257+
258+
// do the objects have the same contents?
259+
alert(JSON.stringify(obj) === JSON.stringify(objCopy)); // true
260+
261+
// are the objects equal?
262+
alert(obj === objCopy); // false (not same reference)
263+
264+
// modifying our initial object does not modify the copy:
265+
obj.d = 4;
266+
alert(JSON.stringify(obj)); // {"a":1,"b":2,"c":3,"d":4}
267+
alert(JSON.stringify(objCopy)); // {"a":1,"b":2,"c":3}
268+
```
269+
270+
This way of copying an object is much shorter than `let objCopy = Object.assign({}, obj);` or for an array `let arrCopy = Object.assign([], arr);` so we prefer to use it whenever we can.
271+
272+
228273
## Summary
229274
230275
When we see `"..."` in the code, it is either rest parameters or the spread syntax.

1-js/06-advanced-functions/03-closure/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Here's a little bit longer code:
182182
Rectangles on the right-hand side demonstrate how the global Lexical Environment changes during the execution:
183183

184184
1. When the script starts, the Lexical Environment is pre-populated with all declared variables.
185-
- Initially, they are in the "Uninitialized" state. That's a special internal state, it means that the engine knows about the variable, but won't allow to use it before `let`. It's almost the same as if the variable didn't exist.
185+
- Initially, they are in the "Uninitialized" state. That's a special internal state, it means that the engine knows about the variable, but it cannot be referenced until it has been declared with `let`. It's almost the same as if the variable didn't exist.
186186
2. Then `let phrase` definition appears. There's no assignment yet, so its value is `undefined`. We can use the variable since this moment.
187187
3. `phrase` is assigned a value.
188188
4. `phrase` changes the value.

1-js/08-prototypes/04-prototype-methods/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ This call makes a truly exact copy of `obj`, including all properties: enumerabl
6565

6666
## Brief history
6767

68-
If we count all the ways to manage `[[Prototype]]`, there are a lot! Many ways to do the same!
68+
If we count all the ways to manage `[[Prototype]]`, there are a lot! Many ways to do the same thing!
6969

7070
Why?
7171

2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Outer corners
22

3-
Outer corners are basically what we get from [elem.getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/DOM/element.getBoundingClientRect).
3+
Outer corners are basically what we get from [elem.getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/DOM/element.getBoundingClientRect).
44

55
Coordinates of the upper-left corner `answer1` and the bottom-right corner `answer2`:
66

2-ui/2-events/02-bubbling-and-capturing/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ There's a property `event.eventPhase` that tells us the number of the phase on w
186186

187187
The event handling process:
188188

189-
- When an event happens -- the most nested element where it happens gets labeled as the "target element" (`event.target`).
190-
- Then the event first moves from the document root down to the `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way.
191-
- Then the event moves from `event.target` up to the root, calling handlers assigned using `on<event>` and `addEventListener` without the 3rd argument or with the 3rd argument `false`.
189+
- Then the event moves down from the document root to `event.target`, calling handlers assigned with `addEventListener(..., true)` on the way (`true` is a shorthand for `{capture: true}`).
190+
- Then handlers are called on the target element itself.
191+
- Then the event bubbles up from `event.target` up to the root, calling handlers assigned using `on<event>` and `addEventListener` without the 3rd argument or with the 3rd argument `false/{capture:false}`.
192192

193193
Each handler can access `event` object properties:
194194

2-ui/2-events/04-default-browser-action/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ The property `event.defaultPrevented` is `true` if the default action was preven
9999

100100
There's an interesting use case for it.
101101

102-
You remember in the chapter <info:bubbling-and-capturing> we talked about `event.stopPropagation()` and why stopping bubbling is bad?
102+
You remember in the chapter <info:bubbling-and-capturing> we talked about `event.stopPropagation()` and why stopping bubbling is bad?
103103

104104
Sometimes we can use `event.defaultPrevented` instead, to signal other event handlers that the event was handled.
105105

2-ui/2-events/05-dispatch-events/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ For instance, here the nested `menu-open` event is processed synchronously, duri
236236
237237
document.addEventListener('menu-open', () => alert('nested'));
238238
</script>
239-
```
239+
```
240240

241241
The output order is: 1 -> nested -> 2.
242242

@@ -263,7 +263,7 @@ If we don't like it, we can either put the `dispatchEvent` (or other event-trigg
263263
264264
document.addEventListener('menu-open', () => alert('nested'));
265265
</script>
266-
```
266+
```
267267

268268
Now `dispatchEvent` runs asynchronously after the current code execution is finished, including `mouse.onclick`, so event handlers are totally separate.
269269

2-ui/3-event-details/1-mouse-events-basics/article.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ But if we track `mousedown` and `mouseup`, then we need it, because these events
6565
There are the three possible values:
6666

6767
- `event.which == 1` -- the left button
68-
- `event.which == 2` - the middle button
69-
- `event.which == 3` - the right button
68+
- `event.which == 2` -- the middle button
69+
- `event.which == 3` -- the right button
7070

7171
The middle button is somewhat exotic right now and is very rarely used.
7272

@@ -116,8 +116,7 @@ For JS-code it means that we should check `if (event.ctrlKey || event.metaKey)`.
116116
```
117117
118118
```warn header="There are also mobile devices"
119-
Keyboard combinations are good as an addition to the workflow. So that if the visitor has a
120-
keyboard -- it works. And if their device doesn't have it -- then there should be another way to do the same.
119+
Keyboard combinations are good as an addition to the workflow. So that if the visitor has a keyboard -- it works. And if their device doesn't have it -- then there should be another way to do the same.
121120
```
122121

123122
## Coordinates: clientX/Y, pageX/Y

0 commit comments

Comments
 (0)