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
There's one more way to create a function. It's rarely used, but sometimes there's no alternative.
4
+
یک راه دیگر برای ساخت یک تابع وجود دارد. به ندرت استفاده میشود، اما گاهی اوقات راه دیگری وجود ندارد.
5
5
6
-
## Syntax
6
+
## سینتکس
7
7
8
-
The syntax for creating a function:
8
+
سینتکس آن برای ساخت یک تابع به صورت زیر است:
9
9
10
10
```js
11
11
let func =newFunction ([arg1, arg2, ...argN], functionBody);
12
12
```
13
13
14
-
The function is created with the arguments `arg1...argN`and the given `functionBody`.
14
+
تابع ساخته شده دارای آرگومانهای `arg1...argN`و بدنهی `functionBody` خواهد بود.
15
15
16
-
It's easier to understand by looking at an example. Here's a function with two arguments:
16
+
این روش با نگاه به یک مثال قابل درکتر است. در زیر تابعی با دو آرگومان ساخته شده:
17
17
18
18
```js run
19
19
let sum =newFunction('a', 'b', 'return a + b');
20
20
21
21
alert( sum(1, 2) ); // 3
22
22
```
23
23
24
-
And here there's a function without arguments, with only the function body:
24
+
و این هم یک تابع بدون آرگومان است، که فقط بدنه دارد:
25
25
26
26
```js run
27
-
let sayHi =newFunction('alert("Hello")');
27
+
let sayHi =newFunction('alert("سلام")');
28
28
29
-
sayHi(); //Hello
29
+
sayHi(); //سلام
30
30
```
31
31
32
-
The major difference from other ways we've seen is that the function is created literally from a string, that is passed at run time.
32
+
تفاوت اصلی از روشهای دیگری که دیدیم این است که تابع در واقع از یک رشتهای ساخته میشود که در زمان اجرا (رانتایم) وارد تابع میشود.
33
33
34
-
All previous declarations required us, programmers, to write the function code in the script.
34
+
همهی روشهای تعریف تابع قبلی ما برنامهنویسها را ملزم به نوشتن کد تابع میکرد.
35
35
36
-
But`new Function`allows to turn any string into a function. For example, we can receive a new function from a server and then execute it:
36
+
اما`new Function`به ما این امکان را میدهد که هر رشتهی دلخواه را به تابع تبدیل کنیم. برای مثال میتوانیم یک تابع را از یک سرور دریافت و سپس آنرا اجرا کنیم:
37
37
38
38
```js
39
-
let str =...receive the code from a server dynamically...
39
+
let str =...کد را به صورت زنده از سرور دریافت کن...
40
40
41
41
let func =newFunction(str);
42
42
func();
43
43
```
44
44
45
-
It is used in very specific cases, like when we receive code from a server, or to dynamically compile a function from a template, in complex web-applications.
45
+
از این روش در روش شرایط خیلی خاص، برای مثال زمانی که کد را از یک سرور دریافت میکنیم، یا کامپایل یک تابع از روی یک الگو به صورت پویا، در وباپلیکیشنهای پیچیده استفاده میشود.
46
46
47
-
## Closure
47
+
## بستار
48
48
49
-
Usually, a function remembers where it was born in the special property `[[Environment]]`. It references the Lexical Environment from where it's created (we covered that in the chapter<info:closure>).
49
+
معمولا، یک تابع به واسطه ویژگی `[[Environment]]` به یاد دارد که کجا متولد شده. این ویژگی به محیط لغوی (lexical environment) از جایی که ساخته شده ارجاع میدهد (در این باره قبلا در بخش<info:closure> صحبت کردهایم).
50
50
51
-
But when a function is created using `new Function`, its `[[Environment]]`is set to reference not the current Lexical Environment, but the global one.
51
+
اما زمانی که تابعی با `new Function` ساخته شود، `[[Environment]]`آن نه به محیط لغوی بلکه به محیط سراسری یا گلوبال اشاره میشود.
52
52
53
-
So, such function doesn't have access to outer variables, only to the global ones.
53
+
در نتیجه، تابع به متغیرهای بیرونی خودش دسترسی ندارد، بلکه فقط به متغیرهای گلوبایل دسترسی دارد.
54
54
55
55
```js run
56
56
functiongetFunc() {
57
-
let value ="test";
57
+
let value ="تست";
58
58
59
59
*!*
60
60
let func =newFunction('alert(value)');
@@ -66,11 +66,11 @@ function getFunc() {
66
66
getFunc()(); // error: value is not defined
67
67
```
68
68
69
-
Compare it with the regular behavior:
69
+
این را با رفتار عادی مقایسه کنید:
70
70
71
71
```js run
72
72
functiongetFunc() {
73
-
let value ="test";
73
+
let value ="تست";
74
74
75
75
*!*
76
76
letfunc=function() { alert(value); };
@@ -79,45 +79,45 @@ function getFunc() {
79
79
return func;
80
80
}
81
81
82
-
getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc
82
+
getFunc()(); // *!*"test"*/!*, از محیط لغوی تابع getFunc
83
83
```
84
84
85
-
This special feature of `new Function`looks strange, but appears very useful in practice.
85
+
این قابلیت ویژهی `new Function`عجیب به نظر میرسد، اما در عمل بسیار کارا است.
86
86
87
-
Imagine that we must create a function from a string. The code of that function is not known at the time of writing the script (that's why we don't use regular functions), but will be known in the process of execution. We may receive it from the server or from another source.
87
+
تصور کنید که ما مجبور هستیم تابعی از یک رشته بسازیم. کد آن تابع در زمان نوشتن کد معلوم نیست (به همین دلیل است در این موقعیت از تابع معمولی استفاده نمیکنیم) ، اما در زمان اجرا کد تابع معلوم خواهد شد. شاید آنرا از سرور یا یک منبع دیگری دریافت کردهایم.
88
88
89
-
Our new function needs to interact with the main script.
89
+
تابع جدید ما نیاز دارد که با کدهای سند اصلی ما تعامل داشته باشد.
90
90
91
-
What if it could access the outer variables?
91
+
اما چه اتفاقی میافتد اگر به متغیرهای بیرونی دسترسی داشته باشد؟
92
92
93
-
The problem is that before JavaScript is published to production, it's compressed using a *minifier* -- a special program that shrinks code by removing extra comments, spaces and -- what's important, renames local variables into shorter ones.
93
+
مشکل این است که قبل از این که جاوااسکریپت برای استفاده منتشر شود، توسط یک *minifier* -- یک برنامه مخصوص که کد را با حذف کامنتها، فاصلهگذاریها و ... فشرده میکند -- فشرده میکند. چیزی که مهم است این است که نام متغیرهای محلی به کلمات کوتاهتری تغییر داده میشوند.
94
94
95
-
For instance, if a function has `let userName`, minifier replaces it with `let a` (or another letter if this one is occupied), and does it everywhere. That's usually a safe thing to do, because the variable is local, nothing outside the function can access it. And inside the function, minifier replaces every mention of it. Minifiers are smart, they analyze the code structure, so they don't break anything. They're not just a dumb find-and-replace.
95
+
برای مثال، اگر یک تابع در بدنهاش شامل `let userName` باشد، آن برنامه کوچکساز (minifier) آنرا با چیزی شبیه `let a` (یا حرفی که تا کنون استفاده نشده باشد) جایگزین میکند. این کار معمولا بدون خطر خواهد بود، زیرا متغیر محلی است و در هیچکجا خارج از تابع به آن دسترسی نخواهند داشت. و درون تابع، کوچکساز هر اسمی از آن را جایگزین میکند. کوچکسازها باهوش عمل میکنند، آنها ساختار کد را ارزیابی میکنند، تا مطمئن شوند چیزی خراب نمیشود. آنها فقط یک پیداکن و جایگزینکن احمق نیستند.
96
96
97
-
So if`new Function`had access to outer variables, it would be unable to find renamed `userName`.
97
+
پس اگر`new Function`به متغیرهای بیرونی دسترسی داشته باشد، عملا نمیتواند متغیر تغییرنام یاقتهی `userName` را پیدا کند.
98
98
99
-
**If`new Function`had access to outer variables, it would have problems with minifiers.**
99
+
**اگر`new Function`به متغیرهای بیرونی دسترسی داشت, با کوچکسازها دچار تداخل و ناسازگاری میشد.**
100
100
101
-
Besides, such code would be architecturally bad and prone to errors.
101
+
غیر از این، چنین کدی از نظر معماری بد و دارای ضعف بوده و احتمالا باعث بروز مشکلاتی میشود.
102
102
103
-
To pass something to a function, created as `new Function`, we should use its arguments.
103
+
برای دادن ورودی به یک تابعی که به وسیله `new Function` ساخته شده، باید از آرگومانهای آن استفاده کنیم.
104
104
105
-
## Summary
105
+
## خلاصه
106
106
107
-
The syntax:
107
+
سینتکس:
108
108
109
109
```js
110
110
let func =newFunction ([arg1, arg2, ...argN], functionBody);
111
111
```
112
112
113
-
For historical reasons, arguments can also be given as a comma-separated list.
113
+
به دلایلی، آرگومانها میتوانند به صورت یک لیست که با کاما جدا شده نیز معرفی شوند.
114
114
115
-
These three declarations mean the same:
115
+
تعاریف زیر، همگی شبیه هم است و یک معنی خواهد داشت:
116
116
117
117
```js
118
-
newFunction('a', 'b', 'return a + b'); //basic syntax
119
-
newFunction('a,b', 'return a + b'); //comma-separated
120
-
newFunction('a , b', 'return a + b'); //comma-separated with spaces
118
+
newFunction('a', 'b', 'return a + b'); //سینتکس معمولی
119
+
newFunction('a,b', 'return a + b'); //جدا شده با کاما
120
+
newFunction('a , b', 'return a + b'); //جدا شده با کاما - و وجود فاصله گذاری
121
121
```
122
122
123
-
Functions created with`new Function`, have `[[Environment]]`referencing the global Lexical Environment, not the outer one. Hence, they cannot use outer variables. But that's actually good, because it insures us from errors. Passing parameters explicitly is a much better method architecturally and causes no problems with minifiers.
123
+
توابعی که با`new Function` ساخته میشوند، `[[Environment]]`آنها به محیط لغوی گلوبال اشاره دارد، نه بیرونی. به همین دلیل، نمیتوانند از متغیرهای بیرونی استفاده کنند، که در واقع چیز خوبیاست، به ما اطمینان میدهد که به اروری برنخواهیم خورد. اینکه به صورت واضح از پارامترهای ورودی استفاده کنیم، از نظر معماری روش بهتری است و هیچ مشکلی را هم رابطه با کوچکسازها ایجاد نخواهد کرد.
0 commit comments