Skip to content

Commit dc4c008

Browse files
authored
Merge pull request #151 from erfanyeganegi/new-function
The "new Function" syntax
2 parents c0c5540 + 00bf90a commit dc4c008

File tree

1 file changed

+40
-40
lines changed
  • 1-js/06-advanced-functions/07-new-function

1 file changed

+40
-40
lines changed
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
11

2-
# The "new Function" syntax
2+
# سینتکس "new Function"
33

4-
There's one more way to create a function. It's rarely used, but sometimes there's no alternative.
4+
یک راه دیگر برای ساخت یک تابع وجود دارد. به ندرت استفاده می‌شود، اما گاهی اوقات راه دیگری وجود ندارد.
55

6-
## Syntax
6+
## سینتکس
77

8-
The syntax for creating a function:
8+
سینتکس آن برای ساخت یک تابع به صورت زیر است:
99

1010
```js
1111
let func = new Function ([arg1, arg2, ...argN], functionBody);
1212
```
1313

14-
The function is created with the arguments `arg1...argN` and the given `functionBody`.
14+
تابع ساخته شده دارای آرگومان‌های `arg1...argN` و بدنه‌ی `functionBody` خواهد بود.
1515

16-
It's easier to understand by looking at an example. Here's a function with two arguments:
16+
این روش با نگاه به یک مثال قابل درک‌تر است. در زیر تابعی با دو آرگومان ساخته شده:
1717

1818
```js run
1919
let sum = new Function('a', 'b', 'return a + b');
2020

2121
alert( sum(1, 2) ); // 3
2222
```
2323

24-
And here there's a function without arguments, with only the function body:
24+
و این هم یک تابع بدون آرگومان است، که فقط بدنه دارد:
2525

2626
```js run
27-
let sayHi = new Function('alert("Hello")');
27+
let sayHi = new Function('alert("سلام")');
2828

29-
sayHi(); // Hello
29+
sayHi(); // سلام
3030
```
3131

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+
تفاوت اصلی از روش‌های دیگری که دیدیم این است که تابع در واقع از یک رشته‌‌ای ساخته می‌شود که در زمان اجرا (ران‌تایم) وارد تابع می‌شود.
3333

34-
All previous declarations required us, programmers, to write the function code in the script.
34+
همه‌ی روش‌های تعریف تابع قبلی ما برنامه‌نویس‌ها را ملزم به نوشتن کد تابع می‌کرد.
3535

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` به ما این امکان را می‌دهد که هر رشته‌ی دلخواه را به تابع تبدیل کنیم. برای مثال می‌توانیم یک تابع را از یک سرور دریافت و سپس آنرا اجرا کنیم:
3737

3838
```js
39-
let str = ... receive the code from a server dynamically ...
39+
let str = ... کد را به صورت زنده از سرور دریافت کن ...
4040

4141
let func = new Function(str);
4242
func();
4343
```
4444

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+
از این روش در روش شرایط خیلی خاص، برای مثال زمانی که کد را از یک سرور دریافت می‌کنیم، یا کامپایل یک تابع از روی یک الگو به صورت پویا، در وب‌اپلیکیشن‌های پیچیده استفاده می‌شود.
4646

47-
## Closure
47+
## بستار
4848

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> صحبت کرده‌ایم).
5050

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]]` آن نه به محیط لغوی بلکه به محیط سراسری یا گلوبال اشاره می‌شود.
5252

53-
So, such function doesn't have access to outer variables, only to the global ones.
53+
در نتیجه، تابع به متغیرهای بیرونی خودش دسترسی ندارد، بلکه فقط به متغیرهای گلوبایل دسترسی دارد.
5454

5555
```js run
5656
function getFunc() {
57-
let value = "test";
57+
let value = "تست";
5858

5959
*!*
6060
let func = new Function('alert(value)');
@@ -66,11 +66,11 @@ function getFunc() {
6666
getFunc()(); // error: value is not defined
6767
```
6868

69-
Compare it with the regular behavior:
69+
این را با رفتار عادی مقایسه کنید:
7070

7171
```js run
7272
function getFunc() {
73-
let value = "test";
73+
let value = "تست";
7474

7575
*!*
7676
let func = function() { alert(value); };
@@ -79,45 +79,45 @@ function getFunc() {
7979
return func;
8080
}
8181

82-
getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc
82+
getFunc()(); // *!*"test"*/!*, از محیط لغوی تابع getFunc
8383
```
8484

85-
This special feature of `new Function` looks strange, but appears very useful in practice.
85+
این قابلیت ویژه‌ی `new Function` عجیب به نظر می‌رسد، اما در عمل بسیار کارا است.
8686

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+
تصور کنید که ما مجبور هستیم تابعی از یک رشته بسازیم. کد آن تابع در زمان نوشتن کد معلوم نیست (به همین دلیل است در این موقعیت از تابع معمولی استفاده نمی‌کنیم) ، اما در زمان اجرا کد تابع معلوم خواهد شد. شاید آنرا از سرور یا یک منبع دیگری دریافت کرده‌ایم.
8888

89-
Our new function needs to interact with the main script.
89+
تابع جدید ما نیاز دارد که با کدهای سند اصلی ما تعامل داشته باشد.
9090

91-
What if it could access the outer variables?
91+
اما چه اتفاقی می‌افتد اگر به متغیرهای بیرونی دسترسی داشته باشد؟
9292

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* -- یک برنامه مخصوص که کد را با حذف کامنت‌ها، فاصله‌گذاری‌ها و ... فشرده می‌کند -- فشرده می‌کند. چیزی که مهم است این است که نام متغیرهای محلی به کلمات کوتاه‌تری تغییر داده‌ می‌شوند.
9494

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` (یا حرفی که تا کنون استفاده نشده باشد) جایگزین می‌کند. این کار معمولا بدون خطر خواهد بود، زیرا متغیر محلی است و در هیچ‌کجا خارج از تابع به آن دسترسی نخواهند داشت. و درون تابع، کوچک‌ساز هر اسمی از آن را جایگزین می‌کند. کوچک‌سازها باهوش عمل می‌کنند، آنها ساختار کد را ارزیابی می‌کنند، تا مطمئن شوند چیزی خراب نمی‌شود. آنها فقط یک پیداکن و جایگزین‌کن احمق نیستند.
9696

97-
So if `new Function` had access to outer variables, it would be unable to find renamed `userName`.
97+
پس اگر `new Function` به متغیرهای بیرونی دسترسی داشته باشد، عملا نمی‌تواند متغیر تغییرنام یاقته‌ی `userName` را پیدا کند.
9898

99-
**If `new Function` had access to outer variables, it would have problems with minifiers.**
99+
**اگر `new Function` به متغیرهای بیرونی دسترسی داشت, با کوچک‌سازها دچار تداخل و ناسازگاری می‌شد.**
100100

101-
Besides, such code would be architecturally bad and prone to errors.
101+
غیر از این، چنین کدی از نظر معماری بد و دارای ضعف بوده و احتمالا باعث بروز مشکلاتی می‌شود.
102102

103-
To pass something to a function, created as `new Function`, we should use its arguments.
103+
برای دادن ورودی به یک تابعی که به وسیله `new Function` ساخته شده، باید از آرگومان‌های آن استفاده کنیم.
104104

105-
## Summary
105+
## خلاصه
106106

107-
The syntax:
107+
سینتکس:
108108

109109
```js
110110
let func = new Function ([arg1, arg2, ...argN], functionBody);
111111
```
112112

113-
For historical reasons, arguments can also be given as a comma-separated list.
113+
به دلایلی،‌ آرگومان‌ها می‌توانند به صورت یک لیست که با کاما جدا شده نیز معرفی شوند.
114114

115-
These three declarations mean the same:
115+
تعاریف زیر، همگی شبیه هم است و یک معنی خواهد داشت:
116116

117117
```js
118-
new Function('a', 'b', 'return a + b'); // basic syntax
119-
new Function('a,b', 'return a + b'); // comma-separated
120-
new Function('a , b', 'return a + b'); // comma-separated with spaces
118+
new Function('a', 'b', 'return a + b'); // سینتکس معمولی
119+
new Function('a,b', 'return a + b'); // جدا شده با کاما
120+
new Function('a , b', 'return a + b'); // جدا شده با کاما - و وجود فاصله گذاری
121121
```
122122

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

Comments
 (0)