Skip to content

Commit 9a0b31d

Browse files
committed
tasks translate
1 parent 4bd67f3 commit 9a0b31d

8 files changed

Lines changed: 43 additions & 45 deletions

File tree

1-js/06-advanced-functions/10-bind/2-write-to-object-after-bind/solution.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
The answer: `null`.
2-
1+
Ответ: `null`.
32

43
```js run
54
function f() {
@@ -13,6 +12,10 @@ let user = {
1312
user.g();
1413
```
1514

16-
The context of a bound function is hard-fixed. There's just no way to further change it.
15+
Контекст связанной функции жестко фиксирован. Изменить однажды привязанный контекст уже нельзя.
16+
17+
Так как вызов идёт в контексте объекта `user`, то внутри функции `g` контекст `this=user`.
18+
Однако, функции `g` совершенно без разницы, какой `this` она получила.
19+
Её единственное предназначение – это передать вызов в `f` вместе с аргументами и ранее указанным контекстом `null`, что она и делает.
1720

18-
So even while we run `user.g()`, the original function is called with `this=null`.
21+
Таким образом, когда мы запускаем `user.g()`, исходная функция вызывается с `this=null`.

1-js/06-advanced-functions/10-bind/2-write-to-object-after-bind/task.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
importance: 5
1+
важность: 5
22

33
---
44

5-
# Bound function as a method
5+
# Связанная функция как метод
66

7-
What will be the output?
7+
Что выведет функция?
88

99
```js
1010
function f() {
@@ -17,4 +17,3 @@ let user = {
1717

1818
user.g();
1919
```
20-
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
The answer: **John**.
1+
Ответ: **Вася**.
22

33
```js run no-beautify
44
function f() {
55
alert(this.name);
66
}
77

8-
f = f.bind( {name: "John"} ).bind( {name: "Pete"} );
8+
f = f.bind( { name: "Вася" } ).bind( { name: "Петя" } );
99

10-
f(); // John
10+
f(); // Вася
1111
```
1212

13-
The exotic [bound function](https://tc39.github.io/ecma262/#sec-bound-function-exotic-objects) object returned by `f.bind(...)` remembers the context (and arguments if provided) only at creation time.
13+
Экзотический объект [bound function](https://tc39.github.io/ecma262/#sec-bound-function-exotic-objects), возвращаемый при первом вызове `f.bind (...)`, запоминает контекст (и аргументы, если они были переданы) только во время создания.
1414

15-
A function cannot be re-bound.
15+
Следующий вызов `bind` будет устанавливать контекст уже для этого объекта. Это ни на что не повлияет.
16+
17+
Функция не может быть повторно связана.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
importance: 5
1+
важность: 5
22

33
---
44

5-
# Second bind
5+
# Повторный bind
66

7-
Can we change `this` by additional binding?
7+
Можем ли мы изменить `this` дополнительным связыванием?
88

9-
What will be the output?
9+
Что выведет этот код?
1010

1111
```js no-beautify
1212
function f() {
1313
alert(this.name);
1414
}
1515

16-
f = f.bind( {name: "John"} ).bind( {name: "Ann" } );
16+
f = f.bind( { name: "Вася" } ).bind( { name: "Петя" } );
1717

1818
f();
1919
```
20-
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
The answer: `undefined`.
2-
3-
The result of `bind` is another object. It does not have the `test` property.
1+
Ответ: `undefined`.
42

3+
Результатом работы `bind` является другой объект. У него уже нет свойства `test`.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
importance: 5
1+
важность: 5
22

33
---
44

5-
# Function property after bind
5+
# Свойство функции после bind
66

7-
There's a value in the property of a function. Will it change after `bind`? Why, elaborate?
7+
В свойство функции записано значение. Изменится ли оно после применения bind? Обоснуйте ответ.
88

99
```js run
1010
function sayHi() {
@@ -14,10 +14,9 @@ sayHi.test = 5;
1414

1515
*!*
1616
let bound = sayHi.bind({
17-
name: "John"
17+
name: "Вася"
1818
});
1919

20-
alert( bound.test ); // what will be the output? why?
20+
alert( bound.test ); // что выведет? почему?
2121
*/!*
2222
```
23-

1-js/06-advanced-functions/10-bind/5-question-use-bind/solution.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
The error occurs because `ask` gets functions `loginOk/loginFail` without the object.
2+
Ошибка происходит потому, что `askPassword` получает функции `loginOk/loginFail` без объекта.
33

4-
When it calls them, they naturally assume `this=undefined`.
4+
Когда они вызываются, то, естественно, предполагают `this=undefined`.
55

6-
Let's `bind` the context:
6+
Используем `bind`, чтобы передать в `askPassword` функции `loginOk/loginFail` с уже привязанным контекстом:
77

88
```js run
99
function askPassword(ok, fail) {
@@ -13,7 +13,7 @@ function askPassword(ok, fail) {
1313
}
1414

1515
let user = {
16-
name: 'John',
16+
name: 'Вася',
1717

1818
loginOk() {
1919
alert(`${this.name} logged in`);
@@ -30,14 +30,13 @@ askPassword(user.loginOk.bind(user), user.loginFail.bind(user));
3030
*/!*
3131
```
3232

33-
Now it works.
33+
Теперь всё работает корректно.
34+
35+
Альтернативное решение - сделать функции-обёртки над `user.loginOk/loginFail`:
3436

35-
An alternative solution could be:
3637
```js
3738
//...
3839
askPassword(() => user.loginOk(), () => user.loginFail());
3940
```
4041

41-
Usually that also works, but may fail in more complex situations where `user` has a chance of being overwritten between the moments of asking and running `() => user.loginOk()`.
42-
43-
42+
Обычно это также работает, но может не сработать в более сложных ситуациях, когда `user` может быть перезаписан между моментами запроса и выполнения `() => user.loginOk()`.

1-js/06-advanced-functions/10-bind/5-question-use-bind/task.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
importance: 5
1+
важность: 5
22

33
---
44

5-
# Ask losing this
5+
# askPassword теряет this
66

7-
The call to `askPassword()` in the code below should check the password and then call `user.loginOk/loginFail` depending on the answer.
7+
Вызов `askPassword()` в приведенном ниже коде должен проверить пароль и затем вызвать `user.loginOk/loginFail` в зависимости от ответа.
88

9-
But it leads to an error. Why?
9+
Однако, его вызов приводит к ошибке. Почему?
1010

11-
Fix the highlighted line for everything to start working right (other lines are not to be changed).
11+
Исправьте выделенную строку, чтобы всё работало (других строк изменять не надо).
1212

1313
```js run
1414
function askPassword(ok, fail) {
@@ -18,7 +18,7 @@ function askPassword(ok, fail) {
1818
}
1919

2020
let user = {
21-
name: 'John',
21+
name: 'Вася',
2222

2323
loginOk() {
2424
alert(`${this.name} logged in`);
@@ -34,5 +34,3 @@ let user = {
3434
askPassword(user.loginOk, user.loginFail);
3535
*/!*
3636
```
37-
38-

0 commit comments

Comments
 (0)