You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Arrow functions are not just a "shorthand" for writing small stuff. They have some very specific and useful features.
5
+
تابعهای کمانی فقط «کوتاهنویسی» برای نوشتن چیزهای کوچک نیستند. آنها ویژگیهایی خاص و کاربردی دارند.
6
6
7
-
JavaScript is full of situations where we need to write a small function that's executed somewhere else.
7
+
جاوااسکریپت پر از موقعیتهایی است که ما نیاز به نوشتن یک تابع کوچک داریم تا جایی دیگر اجرا شود.
8
8
9
-
For instance:
9
+
برایی مثال:
10
10
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
+
- ...و چیزهای دیگر
14
14
15
-
It's in the very spirit of JavaScript to create a function and pass it somewhere.
15
+
اینکه تابعی را بسازیم و آن را جایی دیگر پاس دهیم در ذات جاوااسکریپت است.
16
16
17
-
And in such functions we usually don't want to leave the current context. That's where arrow functions come in handy.
17
+
و معمولا ما نمیخواهیم زمینه کنونی را درون چنین تابعهایی از دست دهیم. اینجا جایی است که تابعهای کمانی بدرد میخورند.
18
18
19
-
## Arrow functions have no "this"
19
+
## تابعهای کمانی "this" ندارند
20
20
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`دسترسی پیدا کنیم، از بیرون دریافت میشود.
22
22
23
-
For instance, we can use it to iterate inside an object method:
23
+
برای مثال، ما میتوانیم از آن برای حلقه زدن درون یک متد شیء استفاده کنیم:
24
24
25
25
```js run
26
26
let group = {
27
-
title:"Our Group",
27
+
title:"گروه ما",
28
28
students: ["John", "Pete", "Alice"],
29
29
30
30
showList() {
@@ -39,19 +39,19 @@ let group = {
39
39
group.showList();
40
40
```
41
41
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`.
43
43
44
-
If we used a "regular" function, there would be an error:
44
+
اگر ما یک تابع «معمولی» استفاده میکردیم، یک ارور دریافت میکردیم:
45
45
46
46
```js run
47
47
let group = {
48
-
title:"Our Group",
48
+
title:"گروه ما",
49
49
students: ["John", "Pete", "Alice"],
50
50
51
51
showList() {
52
52
*!*
53
53
this.students.forEach(function(student) {
54
-
//Error: Cannot read property 'title' of undefined
54
+
//را خواند undefined از 'title' ارور: نمیتوان ویژگی
55
55
alert(this.title+': '+ student);
56
56
});
57
57
*/!*
@@ -61,28 +61,28 @@ let group = {
61
61
group.showList();
62
62
```
63
63
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`دریافت شود.
65
65
66
-
That doesn't affect arrow functions, because they just don't have `this`.
66
+
این موضوع روی تابعهای کمانی تاثیری ندارد چون آنها `this` ندارند.
67
67
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` فراخوانی شوند.
70
70
```
71
71
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)` فراخوانی شده وجود دارد:
74
74
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` درست مانند جستوجوی یک متغیر معمولی انجام میشود: در محیط لغوی بیرونی.
77
77
```
78
78
79
-
## Arrows have no "arguments"
79
+
## تابعهای کمانی "arguments" ندارند
80
80
81
-
Arrow functions also have no `arguments`variable.
81
+
تابعهای کمانی متغیر `arguments`هم ندارند.
82
82
83
-
That's great for decorators, when we need to forward a call with the current `this`and `arguments`.
83
+
زمانی که در دکوراتور ما باید یک فراخوانی را با `this`کنونی و `arguments` ارسال کنیم عالی است.
84
84
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`میلیثانیه تاخیر میاندازد را برمیگرداند:
86
86
87
87
```js run
88
88
functiondefer(f, ms) {
@@ -92,14 +92,14 @@ function defer(f, ms) {
92
92
}
93
93
94
94
functionsayHi(who) {
95
-
alert('Hello,'+ who);
95
+
alert('سلام،'+ who);
96
96
}
97
97
98
98
let sayHiDeferred =defer(sayHi, 2000);
99
-
sayHiDeferred("John"); //Hello, John after 2 seconds
99
+
sayHiDeferred("John"); // John ،بعد از دو ثانیه: سلام
100
100
```
101
101
102
-
The same without an arrow function would look like:
102
+
کد یکسان بدون تابع کمانی میتواند اینگونه باشد:
103
103
104
104
```js
105
105
functiondefer(f, ms) {
@@ -112,15 +112,15 @@ function defer(f, ms) {
112
112
}
113
113
```
114
114
115
-
Here we had to create additional variables `args`and`ctx`so that the function inside `setTimeout`could take them.
115
+
اینجا ما باید متغیرهای اضافی `args`و`ctx`را ایجاد میکردیم تا تابع درون `setTimeout`بتواند آنها را دریافت کند.
116
116
117
-
## Summary
117
+
## خلاصه
118
118
119
-
Arrow functions:
119
+
تابعهای کمانی:
120
120
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> آن را خواهیم خواند
125
125
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