Skip to content

Commit 0845c8e

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-c89ddc5d
2 parents 009264f + c89ddc5 commit 0845c8e

File tree

11 files changed

+102
-19
lines changed

11 files changed

+102
-19
lines changed

1-js/01-getting-started/1-intro/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Let's see what's so special about JavaScript, what we can achieve with it, and w
44

55
## What is JavaScript?
66

7-
*JavaScript* was initially created to *"make web pages alive"*.
7+
*JavaScript* was initially created to "make web pages alive".
88

99
The programs in this language are called *scripts*. They can be written right in a web page's HTML and run automatically as the page loads.
1010

1-js/02-first-steps/13-switch/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Several variants of `case` which share the same code can be grouped.
117117
For example, if we want the same code to run for `case 3` and `case 5`:
118118
119119
```js run no-beautify
120-
let a = 2 + 2;
120+
let a = 3;
121121
122122
switch (a) {
123123
case 4:

1-js/02-first-steps/17-javascript-specials/article.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ let x = 5;
8181
x = "John";
8282
```
8383

84-
There are 7 data types:
84+
There are 8 data types:
8585

8686
- `number` for both floating-point and integer numbers,
87+
- `bigint` for integer numbers of arbitrary length,
8788
- `string` for strings,
8889
- `boolean` for logical values: `true/false`,
8990
- `null` -- a type with a single value `null`, meaning "empty" or "does not exist",

1-js/04-object-basics/04-object-methods/2-check-syntax/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ let user = {
3434
(user.go)() // John
3535
```
3636

37-
Please note that brackets around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
37+
Please note that parentheses around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.

1-js/04-object-basics/04-object-methods/3-why-this/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Here's the explanations.
33

44
1. That's a regular object method call.
55

6-
2. The same, brackets do not change the order of operations here, the dot is first anyway.
6+
2. The same, parentheses do not change the order of operations here, the dot is first anyway.
77

88
3. Here we have a more complex call `(expression).method()`. The call works as if it were split into two lines:
99

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ So, for the task of turning something into an array, `Array.from` tends to be mo
227227
228228
## Get a new copy of an object/array
229229
230-
Remember when we talked about `Object.assign()` [in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in)?
230+
Remember when we talked about `Object.assign()` [in the past](https://javascript.info/object#cloning-and-merging-object-assign)?
231231
232232
It is possible to do the same thing with the spread operator!
233233
234-
```
234+
```js run
235235
let arr = [1, 2, 3];
236236
let arrCopy = [...arr]; // spread the array into a list of parameters
237237
// then put the result into a new array
@@ -250,7 +250,7 @@ alert(arrCopy); // 1, 2, 3
250250
251251
Note that it is possible to do the same thing to make a copy of an object:
252252
253-
```
253+
```js run
254254
let obj = { a: 1, b: 2, c: 3 };
255255
let objCopy = { ...obj }; // spread the object into a list of parameters
256256
// then return the result in a new object

1-js/09-classes/01-class/article.md

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,17 @@ class User {
298298
new User().sayHi();
299299
```
300300

301-
## Class properties
301+
## Class fields
302302

303303
```warn header="Old browsers may need a polyfill"
304-
Class-level properties are a recent addition to the language.
304+
Class fields are a recent addition to the language.
305305
```
306306

307-
In the example above, `User` only had methods. Let's add a property:
307+
Previously, classes only had methods.
308+
309+
"Class fields" is a syntax that allows to add any properties.
310+
311+
For instance, let's add `name` property to `class User`:
308312

309313
```js run
310314
class User {
@@ -323,7 +327,86 @@ alert(User.prototype.sayHi); // placed in User.prototype
323327
alert(User.prototype.name); // undefined, not placed in User.prototype
324328
```
325329

326-
The property `name` is not placed into `User.prototype`. Instead, it is created by `new` before calling the constructor, it's a property of the object itself.
330+
The important thing about class fields is that they are set on individual objects, not `User.prototype`.
331+
332+
Technically, they are processed after the constructor has done it's job.
333+
334+
### Making bound methods with class fields
335+
336+
As demonstrated in the chapter <info:bind> functions in JavaScript have a dynamic `this`. It depends on the context of the call.
337+
338+
So if an object method is passed around and called in another context, `this` won't be a reference to its object any more.
339+
340+
For instance, this code will show `undefined`:
341+
342+
```js run
343+
class Button {
344+
constructor(value) {
345+
this.value = value;
346+
}
347+
348+
click() {
349+
alert(this.value);
350+
}
351+
}
352+
353+
let button = new Button("hello");
354+
355+
*!*
356+
setTimeout(button.click, 1000); // undefined
357+
*/!*
358+
```
359+
360+
The problem is called "losing `this`".
361+
362+
There are two approaches to fixing it, as discussed in the chapter <info:bind>:
363+
364+
1. Pass a wrapper-function, such as `setTimeout(() => button.click(), 1000)`.
365+
2. Bind the method to object, e.g. in the constructor:
366+
367+
```js run
368+
class Button {
369+
constructor(value) {
370+
this.value = value;
371+
*!*
372+
this.click = this.click.bind(this);
373+
*/!*
374+
}
375+
376+
click() {
377+
alert(this.value);
378+
}
379+
}
380+
381+
let button = new Button("hello");
382+
383+
*!*
384+
setTimeout(button.click, 1000); // hello
385+
*/!*
386+
```
387+
388+
Class fields provide a more elegant syntax for the latter solution:
389+
390+
```js run
391+
class Button {
392+
constructor(value) {
393+
this.value = value;
394+
}
395+
*!*
396+
click = () => {
397+
alert(this.value);
398+
}
399+
*/!*
400+
}
401+
402+
let button = new Button("hello");
403+
404+
setTimeout(button.click, 1000); // hello
405+
```
406+
407+
The class field `click = () => {...}` creates an independent function on each `Button` object, with `this` bound to the object. Then we can pass `button.click` around anywhere, and it will be called with the right `this`.
408+
409+
That's especially useful in browser environment, when we need to setup a method as an event listener.
327410

328411
## Summary
329412

1-js/99-js-misc/03-currying-partials/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ alert( curriedSum(1)(2) ); // 3
3939
As you can see, the implementation is straightforward: it's just two wrappers.
4040

4141
- The result of `curry(func)` is a wrapper `function(a)`.
42-
- When it is called like `sum(1)`, the argument is saved in the Lexical Environment, and a new wrapper is returned `function(b)`.
42+
- When it is called like `curriedSum(1)`, the argument is saved in the Lexical Environment, and a new wrapper is returned `function(b)`.
4343
- Then this wrapper is called with `2` as an argument, and it passes the call to the original `sum`.
4444

4545
More advanced implementations of currying, such as [_.curry](https://lodash.com/docs#curry) from lodash library, return a wrapper that allows a function to be called both normally and partially:

2-ui/4-forms-controls/1-form-elements/article.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ That's usually not a problem, because we rarely change names of form elements.
125125
126126
## Backreference: element.form
127127
128-
For any element, the form is available as `element.form`. So a form references all elements, and elements
129-
reference the form.
128+
For any element, the form is available as `element.form`. So a form references all elements, and elements reference the form.
130129
131130
Here's the picture:
132131

2-ui/5-loading/03-onload-onerror/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ There are three levels of cross-origin access:
169169

170170
1. **No `crossorigin` attribute** -- access prohibited.
171171
2. **`crossorigin="anonymous"`** -- access allowed if the server responds with the header `Access-Control-Allow-Origin` with `*` or our origin. Browser does not send authorization information and cookies to remote server.
172-
3. **`crossorigin="use-credentials"`** -- access allowed if the server sends back the header `Access-Control-Allow-Origin` with our origin and `Access-Control-Allow-Credentials: true`. Browser sends authorization information and cookies to remote server.
172+
3. **`crossorigin="use-credentials"`** -- access allowed if the server sends back the header `Access-Control-Allow-Origin` with our origin and `Access-Control-Allow-Credentials: true`. Browser sends authorization information and cookies to remote server.
173173

174174
```smart
175175
You can read more about cross-origin access in the chapter <info:fetch-crossorigin>. It describes the `fetch` method for network requests, but the policy is exactly the same.

0 commit comments

Comments
 (0)