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
هناك طريقة أخرى لبناء الدالة. هذه الطريقة نادرة الإستخدام لكن أحياناً لا يوجد بديل لها.
3
4
4
-
There's one more way to create a function. It's rarely used, but sometimes there's no alternative.
5
+
## تركيب الجملة
5
6
6
-
## Syntax
7
-
8
-
The syntax for creating a function:
7
+
تركيب جملة بناء الدالة:
9
8
10
9
```js
11
10
let func =newFunction ([arg1, arg2, ...argN], functionBody);
12
11
```
13
12
14
-
The function is created with the arguments `arg1...argN`and the given`functionBody`.
13
+
بهذا التركيب تم صُنع دالة وإرسال لها العوامل الأتية `arg1...argN`و إرسال لها جسم الدالة الذي يحتوي علي ما نريد فعله بداخلها`functionBody`.
15
14
16
-
It's easier to understand by looking at an example. Here's a function with two arguments:
15
+
دعنا نبسط الأمر بمثال لدالة تحتاج إلى عاملين:
17
16
18
17
```js run
19
18
let sum =newFunction('a', 'b', 'return a + b');
20
19
21
20
alert( sum(1, 2) ); // 3
22
21
```
23
22
24
-
And here there's a function without arguments, with only the function body:
23
+
هذا مثال أخر لدالة لا تحتاج إلى عوامل فقط نرسل لها جسم الدالة:
25
24
26
25
```js run
27
26
let sayHi =newFunction('alert("Hello")');
28
27
29
28
sayHi(); // Hello
30
29
```
30
+
الفرق الأساسي بين هذه الطريقة وباقي الطرق التي تبني الدالة هو أن الدالة تُبني من نص `string` يمكن أن نرسله للدالة أثناء تشغيل البرنامج.
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
+
لكن كلمة دالة جديدة `new Function` تتيح لنا تحويل النص `string` إلى دالة
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
+
مثلا يمكننا أن نستلم الدالة الجديدة من الخادم البعيد ثم ننفذها:
37
37
38
38
```js
39
39
let str =... receive the code from a server dynamically ...
@@ -42,15 +42,14 @@ let func = new Function(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
47
## Closure
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]]`لم تعد تشير إلى الحسد اللغوي Lexical Environment كما ذكرنا في الأعلي وإنما تشير الحدس الشامل إلى `Global Lexical Enviroment`.
52
52
53
-
So, such function doesn't have access to outer variables, only to the global ones.
54
53
55
54
```js run
56
55
functiongetFunc() {
@@ -66,7 +65,7 @@ function getFunc() {
66
65
getFunc()(); // error: value is not defined
67
66
```
68
67
69
-
Compare it with the regular behavior:
68
+
قارن الأن بينه وبين الطريقة التقليدية:
70
69
71
70
```js run
72
71
functiongetFunc() {
@@ -82,42 +81,44 @@ function getFunc() {
82
81
getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc
83
82
```
84
83
85
-
This special feature of `new Function` looks strange, but appears very useful in practice.
84
+
هذه الخاصية الموجودة في دالة جديدة `new Function` تبدو غريبة, لكنها عند التطبيق مفيدة جداً.
85
+
86
+
تخيل أننا نريد بناء دالة من نص `string`. الكود الخاص بالدالة الأن غير معروف في الوقت الذي تكتب فيه البرنامج ( لهذا السبب لا نستخدم الطريقة التقليدية للدالة ) لكن سيكون معروف أثناء التشغيل. ومن الممكن أن نستلم كود الدالة من الخادم البعيد أو من مصدر أخر.
86
87
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.
88
+
دالتنا الجديدة تحتاج إلى التفاعل مع الكود الأساسي.
88
89
89
-
Our new function needs to interact with the main script.
90
+
لكن ماذا سيحدث إذا استطاعت الدالة الوصول إلى المتغيرات الخارجية ولم تكن تمتلك الخاصية المذكورة في الأعلي؟
90
91
91
-
What if it could access the outer variables?
92
+
المشكلة هي أن قبل أن يتم نشر مشروع الجافاسكريبت الخاص بك للإنتاج. يتم ضغطه عن طريق إستخدام شئ يسمي -- *minifier* -- يعتبر هذا البرنامج خاص بتقليص حجم الكود الخاص بك عن طريق مسح الزيادات مثل التعليقات و المسافات و الأهم من ذلك أنه يغير أسماء المتغيرات المحلية الطويلة إلى أسماء متغيرات أقصر.
92
93
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.
94
+
مثلاً اذاً كانت دالة تحتوي علي `let userName` الـ -- *minifier* -- يحولها إلى `let a` (أو أي شئ أخر إذا كان هذا الأسم غير متاح), ويقوم بهذا في كل مكان تم ذكر فيه هذا المتغير وهو شئ آمن لأن المتغير يعتبر محلي داخل الدالة ولا يستطيع أي شئ خارج الدالة الوصول إليه, وداخل الدالة يغير الـ -- *minifier* -- كل مرة ذكر فيها الأسم. *minifier* يعتبر ذكي لأنه يحلل تركيب الكود لكي لايعطل شئ وليس فقط القيام بالتبديل.
94
95
95
-
For instance, if a function has `let userName`, minifier replaces it `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.
96
+
لذلك إن استطاعت الدالة الجديدة `new Function` الوصول إلى المتغيرات الخارجية, الـ*minifier* لن يستطيع إيجاد إسم `userName` وتغيره.
96
97
97
-
So if `new Function`had access to outer variables, it would be unable to find renamed `userName`.
98
+
إذا استطاعت الدالة الجديدة `new Function`الوصول للمتغبرات الخارجية ستكون هذه مشكلة كبير قد تعطل البرنامج لهذا السبب هي زُودت بخاصية عدم الوصول إلى المتغيرات الخارجية الذي ذكرناه بالأعلي
98
99
99
-
**If `new Function` had access to outer variables, it would have problems with minifiers.**
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.
104
103
105
-
## Summary
104
+
## الملخص
106
105
107
-
The syntax:
106
+
طريقة تركيب الجملة:
108
107
109
108
```js
110
109
let func =newFunction ([arg1, arg2, ...argN], functionBody);
111
110
```
112
111
113
-
For historical reasons, arguments can also be given as a comma-separated list.
112
+
لأسباب تاريخية, العوامل يمكن أن نرسلها علي شكل قائمة مفصولة بفاصلات.
113
+
114
+
لاحظ أن الثلاث توضيحات القادة كلهم يحملون نفس المعني:
114
115
115
-
These three declarations mean the same:
116
116
117
117
```js
118
118
newFunction('a', 'b', 'return a + b'); // basic syntax
119
119
newFunction('a,b', 'return a + b'); // comma-separated
120
120
newFunction('a , b', 'return a + b'); // comma-separated with spaces
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]]` التي تشير إلى الحدس الشامل "global Lexical Environment", وليس الحدس الخارجي. ولذلك هذه الدالة لاتستطيع الوصول للمتغيرات الخارجية ولكن هذا شئ جيد لانه لولا حدوثه ستحدث أخطاء كثيرة جداً عند تقليص الكود. وعند الحاجة إلى متغير خارجي نرسله علي شكل عامل للدالة ذلك يمنع كل المشاكل التي ذكرناها في الأعلي.
0 commit comments