Skip to content

Commit acad004

Browse files
authored
Merge pull request #166 from mahdiHashemi14/master
Arrow functions revisited
2 parents 97f4cba + 1152e75 commit acad004

File tree

1 file changed

+41
-41
lines changed
  • 1-js/06-advanced-functions/12-arrow-functions

1 file changed

+41
-41
lines changed
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
# Arrow functions revisited
1+
# سرکشی دوباره از تابع‌های کمانی
22

3-
Let's revisit arrow functions.
3+
بیایید دوباره سری به تابع‌های کمانی بزنیم.
44

5-
Arrow functions are not just a "shorthand" for writing small stuff. They have some very specific and useful features.
5+
تابع‌های کمانی فقط «کوتاه‌نویسی» برای نوشتن چیزهای کوچک نیستند. آن‌ها ویژگی‌هایی خاص و کاربردی دارند.
66

7-
JavaScript is full of situations where we need to write a small function that's executed somewhere else.
7+
جاوااسکریپت پر از موقعیت‌هایی است که ما نیاز به نوشتن یک تابع کوچک داریم تا جایی دیگر اجرا شود.
88

9-
For instance:
9+
برایی مثال:
1010

11-
- `arr.forEach(func)` -- `func` is executed by `forEach` for every array item.
12-
- `setTimeout(func)` -- `func` is executed by the built-in scheduler.
13-
- ...there are more.
11+
- `arr.forEach(func)` -- `func` برای هر المان آرایه توسط `forEach` اجرا می‌شود.
12+
- `setTimeout(func)` -- `func` توسط زمان‌بند درونی اجرا می‌شود.
13+
- ...و چیزهای دیگر
1414

15-
It's in the very spirit of JavaScript to create a function and pass it somewhere.
15+
اینکه تابعی را بسازیم و آن را جایی دیگر پاس دهیم در ذات جاوااسکریپت است.
1616

17-
And in such functions we usually don't want to leave the current context. That's where arrow functions come in handy.
17+
و معمولا ما نمی‌خواهیم زمینه کنونی را درون چنین تابع‌هایی از دست دهیم. اینجا جایی است که تابع‌های کمانی بدرد می‌خورند.
1818

19-
## Arrow functions have no "this"
19+
## تابع‌های کمانی "this" ندارند
2020

21-
As we remember from the chapter <info:object-methods>, arrow functions do not have `this`. If `this` is accessed, it is taken from the outside.
21+
همانطور که از فصل <info:object-methods> به یاد داریم، تابع‌های کمانی `this` ندارند. اگر به `this` دسترسی پیدا کنیم، از بیرون دریافت می‌شود.
2222

23-
For instance, we can use it to iterate inside an object method:
23+
برای مثال، ما می‌توانیم از آن برای حلقه زدن درون یک متد شیء استفاده کنیم:
2424

2525
```js run
2626
let group = {
27-
title: "Our Group",
27+
title: "گروه ما",
2828
students: ["John", "Pete", "Alice"],
2929

3030
showList() {
@@ -39,19 +39,19 @@ let group = {
3939
group.showList();
4040
```
4141

42-
Here in `forEach`, the arrow function is used, so `this.title` in it is exactly the same as in the outer method `showList`. That is: `group.title`.
42+
اینجا درون `forEach`، تابع کمانی استفاده شده است پس `this.title` درون آن انگار دقیقا درون متد بیرونی `showList` است. یعنی: `group.title`.
4343

44-
If we used a "regular" function, there would be an error:
44+
اگر ما یک تابع «معمولی» استفاده می‌کردیم، یک ارور دریافت می‌کردیم:
4545

4646
```js run
4747
let group = {
48-
title: "Our Group",
48+
title: "گروه ما",
4949
students: ["John", "Pete", "Alice"],
5050

5151
showList() {
5252
*!*
5353
this.students.forEach(function(student) {
54-
// Error: Cannot read property 'title' of undefined
54+
// را خواند undefined از 'title' ارور: نمی‌توان ویژگی
5555
alert(this.title + ': ' + student);
5656
});
5757
*/!*
@@ -61,28 +61,28 @@ let group = {
6161
group.showList();
6262
```
6363

64-
The error occurs because `forEach` runs functions with `this=undefined` by default, so the attempt to access `undefined.title` is made.
64+
به دلیل اینکه به طور پیش‌فرض `forEach` با `this=undefined` تابع را اجرا می‌کند ارور ایجاد می‌شود پس سعی می‌شود که `undefined.title` دریافت شود.
6565

66-
That doesn't affect arrow functions, because they just don't have `this`.
66+
این موضوع روی تابع‌های کمانی تاثیری ندارد چون آن‌ها `this` ندارند.
6767

68-
```warn header="Arrow functions can't run with `new`"
69-
Not having `this` naturally means another limitation: arrow functions can't be used as constructors. They can't be called with `new`.
68+
```warn header="تابع‌های کمانی نمی‌توانند با `new` اجرا شوند"
69+
نداشتن `this` به طور طبیعی به معنی محدودیت دیگری هم هست: تابع‌های کمانی نمی‌توانند به عنوان سازنده استفاده شوند. آن‌ها نمی‌توانند با `new` فراخوانی شوند.
7070
```
7171
72-
```smart header="Arrow functions VS bind"
73-
There's a subtle difference between an arrow function `=>` and a regular function called with `.bind(this)`:
72+
```smart header="تابع‌های کمانی در مقابل bind"
73+
یک تفاوت جزئی بین یک تابع کمانی `<=` و یک تابع معمولی که با `.bind(this)` فراخوانی شده وجود دارد:
7474
75-
- `.bind(this)` creates a "bound version" of the function.
76-
- The arrow `=>` doesn't create any binding. The function simply doesn't have `this`. The lookup of `this` is made exactly the same way as a regular variable search: in the outer lexical environment.
75+
- `.bind(this)` یک «نسخه پیوند زده شده» از تابع را می‌سازد.
76+
- کمان `<=` چیزی را پیوند نمی‌زند. تابع حقیقتا `this` ندارد. جست‌و‌جوی `this` درست مانند جست‌و‌جوی یک متغیر معمولی انجام می‌شود: در محیط لغوی بیرونی.
7777
```
7878

79-
## Arrows have no "arguments"
79+
## تابع‌های کمانی "arguments" ندارند
8080

81-
Arrow functions also have no `arguments` variable.
81+
تابع‌های کمانی متغیر `arguments` هم ندارند.
8282

83-
That's great for decorators, when we need to forward a call with the current `this` and `arguments`.
83+
زمانی که در دکوراتور ما باید یک فراخوانی را با `this` کنونی و `arguments` ارسال کنیم عالی است.
8484

85-
For instance, `defer(f, ms)` gets a function and returns a wrapper around it that delays the call by `ms` milliseconds:
85+
برای مثال، `defer(f, ms)` یک تابع دریافت می‌کند و دربرگیرنده‌ای دور آن که فراخوانی را به اندازه `ms` میلی‌ثانیه تاخیر می‌اندازد را برمی‌گرداند:
8686

8787
```js run
8888
function defer(f, ms) {
@@ -92,14 +92,14 @@ function defer(f, ms) {
9292
}
9393

9494
function sayHi(who) {
95-
alert('Hello, ' + who);
95+
alert('سلام، ' + who);
9696
}
9797

9898
let sayHiDeferred = defer(sayHi, 2000);
99-
sayHiDeferred("John"); // Hello, John after 2 seconds
99+
sayHiDeferred("John"); // John ،بعد از دو ثانیه: سلام
100100
```
101101

102-
The same without an arrow function would look like:
102+
کد یکسان بدون تابع کمانی می‌تواند اینگونه باشد:
103103

104104
```js
105105
function defer(f, ms) {
@@ -112,15 +112,15 @@ function defer(f, ms) {
112112
}
113113
```
114114

115-
Here we had to create additional variables `args` and `ctx` so that the function inside `setTimeout` could take them.
115+
اینجا ما باید متغیرهای اضافی `args` و `ctx` را ایجاد می‌کردیم تا تابع درون `setTimeout` بتواند آن‌ها را دریافت کند.
116116

117-
## Summary
117+
## خلاصه
118118

119-
Arrow functions:
119+
تابع‌های کمانی:
120120

121-
- Do not have `this`
122-
- Do not have `arguments`
123-
- Can't be called with `new`
124-
- They also don't have `super`, but we didn't study it yet. We will on the chapter <info:class-inheritance>
121+
- دارای `this` نیستند
122+
- دارای `arguments` نیستند
123+
- نمی‌توانند همراه با `new` فراخوانی شوند
124+
- همچنین آن‌ها `super` ندارند اما هنوز آن را نخوانده‌ایم. در فصل <info:class-inheritance> آن را خواهیم خواند
125125

126-
That's because they are meant for short pieces of code that do not have their own "context", but rather work in the current one. And they really shine in that use case.
126+
به دلیل اینکه آن‌ها برای قطعه‌های کوتاهی از کد در نظر گرفته شده‌اند که «زمینه(context)» خودشان را ندارند و در زمینه کنونی کار می‌کنند. و آن‌ها واقعا در این مورد استفاده می‌درخشند.

0 commit comments

Comments
 (0)