Skip to content

Commit 97f4cba

Browse files
authored
Merge pull request #165 from mahdiHashemi14/master
Function binding
2 parents 4325e92 + e0bbbfb commit 97f4cba

File tree

11 files changed

+132
-132
lines changed

11 files changed

+132
-132
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: `null`.
1+
جواب: `null`.
22

33

44
```js run
@@ -13,6 +13,6 @@ let user = {
1313
user.g();
1414
```
1515

16-
The context of a bound function is hard-fixed. There's just no way to further change it.
16+
زمینه‌ی تابع پیوند زده شده به طور قطعی ثابت شده. راهی برای تغییر بیشتر آن وجود ندارد..
1717

18-
So even while we run `user.g()`, the original function is called with `this=null`.
18+
پس حتی زمانی که ما `user.g()` را اجرا می‌کنیم، تابع اصلی با `this=null` فراخوانی می‌شود.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 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() {

1-js/06-advanced-functions/10-bind/3-second-bind/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: **John**.
1+
جواب: **John**.
22

33
```js run no-beautify
44
function f() {
@@ -10,6 +10,6 @@ f = f.bind( {name: "John"} ).bind( {name: "Pete"} );
1010
f(); // John
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+
شیء بیگانه [تابع پیوند زده شده](https://tc39.github.io/ecma262/#sec-bound-function-exotic-objects) که توسط `f.bind(...)` برگردانده شده، زمینه (و در صورت قرار دادن، آرگومان‌ها) را فقط در زمان ایجاد شدن به یاد می‌سپارد.
1414

15-
A function cannot be re-bound.
15+
یک تابع نمی‌تواند دوباره پیوند زده شود.

1-js/06-advanced-functions/10-bind/3-second-bind/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 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() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: `undefined`.
1+
جواب: `undefined`.
22

3-
The result of `bind` is another object. It does not have the `test` property.
3+
نتیجه `bind` شیء دیگری است. آن شیء ویژگی `test` را ندارد.
44

1-js/06-advanced-functions/10-bind/4-function-property-after-bind/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 5
22

33
---
44

5-
# Function property after bind
5+
# ویژگی تابع بعد از پیوند زدن
66

7-
There's a value in the property of a function. Will it change after `bind`? Why, or why not?
7+
یک مقدار در ویژگی تابعی وجود دارد. آیا بعد از `bind` تغییر می‌کند؟ چرا یا چرا نه؟
88

99
```js run
1010
function sayHi() {
@@ -17,7 +17,7 @@ let bound = sayHi.bind({
1717
name: "John"
1818
});
1919

20-
alert( bound.test ); // what will be the output? why?
20+
alert( bound.test ); // خروجی چه خواهد بود؟ چرا؟
2121
*/!*
2222
```
2323

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

2-
The error occurs because `ask` gets functions `loginOk/loginFail` without the object.
2+
به دلیل اینکه `ask` تابع‌های `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` پیوند بزنیم:
77

88
```js run
99
function askPassword(ok, fail) {
10-
let password = prompt("Password?", '');
10+
let password = prompt("رمز؟", '');
1111
if (password == "rockstar") ok();
1212
else fail();
1313
}
@@ -16,11 +16,11 @@ let user = {
1616
name: 'John',
1717

1818
loginOk() {
19-
alert(`${this.name} logged in`);
19+
alert(`${this.name} وارد شد`);
2020
},
2121

2222
loginFail() {
23-
alert(`${this.name} failed to log in`);
23+
alert(`${this.name} نتوانست وارد شود`);
2424
},
2525

2626
};
@@ -30,14 +30,14 @@ askPassword(user.loginOk.bind(user), user.loginFail.bind(user));
3030
*/!*
3131
```
3232

33-
Now it works.
33+
حالا کار می‌کند.
3434

35-
An alternative solution could be:
35+
راه‌حل جایگزین می‌تواند این باشد:
3636
```js
3737
//...
3838
askPassword(() => user.loginOk(), () => user.loginFail());
3939
```
4040

41-
Usually that also works and looks good.
41+
معمولا این راه‌حل هم کار می‌کند و ظاهر خوبی دارد.
4242

43-
It's a bit less reliable though in more complex situations where `user` variable might change *after* `askPassword` is called, but *before* the visitor answers and calls `() => user.loginOk()`.
43+
اگرچه این کد در موقعیت‌های پیچیده‌تر کمتر قابل اطمینان است، زمانی که متغیر `user` ممکن است *بعد از* اینکه `askPassword` فراخوانی شود و *قبل از* اینکه کاربر جواب بدهد و `() => user.loginOk()` را فرا بخواند، تغییر کند.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 5
22

33
---
44

5-
# Fix a function that loses "this"
5+
# تابعی که "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) {
15-
let password = prompt("Password?", '');
15+
let password = prompt("رمز؟", '');
1616
if (password == "rockstar") ok();
1717
else fail();
1818
}
@@ -21,11 +21,11 @@ let user = {
2121
name: 'John',
2222

2323
loginOk() {
24-
alert(`${this.name} logged in`);
24+
alert(`${this.name} وارد شد`);
2525
},
2626

2727
loginFail() {
28-
alert(`${this.name} failed to log in`);
28+
alert(`${this.name} نتوانست وارد شود`);
2929
},
3030

3131
};

1-js/06-advanced-functions/10-bind/6-ask-partial/solution.md

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

22

3-
1. Either use a wrapper function, an arrow to be concise:
3+
1. برای کوتاه بودن یا از تابع دربرگیرنده استفاده کنید یا از تابع کمانی:
44

55
```js
66
askPassword(() => user.login(true), () => user.login(false));
77
```
88

9-
Now it gets `user` from outer variables and runs it the normal way.
9+
حالا `user` را از متغیرهای بیرونی دریافت می‌کند و به صورت معمولی آن را اجرا می‌شود.
1010

11-
2. Or create a partial function from `user.login` that uses `user` as the context and has the correct first argument:
11+
2. یا یک تابع جزئی از `user.login` بسازید که از `user` به عنوان زمینه استفاده می‌کند و آرگومان اول درست را دارد:
1212

1313

1414
```js

1-js/06-advanced-functions/10-bind/6-ask-partial/task.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 5
22

33
---
44

5-
# Partial application for login
5+
# کاربرد تابع جزئی برای وارد شدن
6+
7+
این تمرین نوع پیچیده‌تر <info:task/question-use-bind> است.
68

7-
The task is a little more complex variant of <info:task/question-use-bind>.
9+
شیء `user` تغییر داده شد. حالا به جای دو تابع `loginOk/loginFail`، یک تابع `user.login(true/false)` دارد.
810

9-
The `user` object was modified. Now instead of two functions `loginOk/loginFail`, it has a single function `user.login(true/false)`.
10-
11-
What should we pass `askPassword` in the code below, so that it calls `user.login(true)` as `ok` and `user.login(false)` as `fail`?
11+
برای اینکه `askPassword` در کد پایین، تابع `user.login(true)` را به عنوان `ok` و `user.login(false)` را به عنوان `fail` فراخوانی کند باید چه کار کنیم؟
1212

1313
```js
1414
function askPassword(ok, fail) {
15-
let password = prompt("Password?", '');
15+
let password = prompt("رمز؟", '');
1616
if (password == "rockstar") ok();
1717
else fail();
1818
}
@@ -21,7 +21,7 @@ let user = {
2121
name: 'John',
2222

2323
login(result) {
24-
alert( this.name + (result ? ' logged in' : ' failed to log in') );
24+
alert( this.name + (result ? ' وارد شد' : ' نتوانست وارد شود') );
2525
}
2626
};
2727

@@ -30,5 +30,5 @@ askPassword(?, ?); // ?
3030
*/!*
3131
```
3232
33-
Your changes should only modify the highlighted fragment.
33+
تغییرات شما فقط باید قطعه برجسته شده را تغییر دهد.
3434

0 commit comments

Comments
 (0)