Skip to content

Commit c5adfd4

Browse files
committed
Translate new-function to Ar
1 parent d962c6d commit c5adfd4

1 file changed

Lines changed: 32 additions & 31 deletions

File tree

  • 1-js/06-advanced-functions/07-new-function
Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1+
# تركيب جملة دالة جديدة "new Function"
12

2-
# The "new Function" syntax
3+
هناك طريقة أخرى لبناء الدالة. هذه الطريقة نادرة الإستخدام لكن أحياناً لا يوجد بديل لها.
34

4-
There's one more way to create a function. It's rarely used, but sometimes there's no alternative.
5+
## تركيب الجملة
56

6-
## Syntax
7-
8-
The syntax for creating a function:
7+
تركيب جملة بناء الدالة:
98

109
```js
1110
let func = new Function ([arg1, arg2, ...argN], functionBody);
1211
```
1312

14-
The function is created with the arguments `arg1...argN` and the given `functionBody`.
13+
بهذا التركيب تم صُنع دالة وإرسال لها العوامل الأتية `arg1...argN` و إرسال لها جسم الدالة الذي يحتوي علي ما نريد فعله بداخلها `functionBody`.
1514

16-
It's easier to understand by looking at an example. Here's a function with two arguments:
15+
دعنا نبسط الأمر بمثال لدالة تحتاج إلى عاملين:
1716

1817
```js run
1918
let sum = new Function('a', 'b', 'return a + b');
2019

2120
alert( sum(1, 2) ); // 3
2221
```
2322

24-
And here there's a function without arguments, with only the function body:
23+
هذا مثال أخر لدالة لا تحتاج إلى عوامل فقط نرسل لها جسم الدالة:
2524

2625
```js run
2726
let sayHi = new Function('alert("Hello")');
2827

2928
sayHi(); // Hello
3029
```
30+
الفرق الأساسي بين هذه الطريقة وباقي الطرق التي تبني الدالة هو أن الدالة تُبني من نص `string` يمكن أن نرسله للدالة أثناء تشغيل البرنامج.
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+
لكن كلمة دالة جديدة `new Function` تتيح لنا تحويل النص `string` إلى دالة
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+
مثلا يمكننا أن نستلم الدالة الجديدة من الخادم البعيد ثم ننفذها:
3737

3838
```js
3939
let str = ... receive the code from a server dynamically ...
@@ -42,15 +42,14 @@ 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

4747
## Closure
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]]` لم تعد تشير إلى الحسد اللغوي Lexical Environment كما ذكرنا في الأعلي وإنما تشير الحدس الشامل إلى `Global Lexical Enviroment`.
5252

53-
So, such function doesn't have access to outer variables, only to the global ones.
5453

5554
```js run
5655
function getFunc() {
@@ -66,7 +65,7 @@ function getFunc() {
6665
getFunc()(); // error: value is not defined
6766
```
6867

69-
Compare it with the regular behavior:
68+
قارن الأن بينه وبين الطريقة التقليدية:
7069

7170
```js run
7271
function getFunc() {
@@ -82,42 +81,44 @@ function getFunc() {
8281
getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc
8382
```
8483

85-
This special feature of `new Function` looks strange, but appears very useful in practice.
84+
هذه الخاصية الموجودة في دالة جديدة `new Function` تبدو غريبة, لكنها عند التطبيق مفيدة جداً.
85+
86+
تخيل أننا نريد بناء دالة من نص `string`. الكود الخاص بالدالة الأن غير معروف في الوقت الذي تكتب فيه البرنامج ( لهذا السبب لا نستخدم الطريقة التقليدية للدالة ) لكن سيكون معروف أثناء التشغيل. ومن الممكن أن نستلم كود الدالة من الخادم البعيد أو من مصدر أخر.
8687

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+
دالتنا الجديدة تحتاج إلى التفاعل مع الكود الأساسي.
8889

89-
Our new function needs to interact with the main script.
90+
لكن ماذا سيحدث إذا استطاعت الدالة الوصول إلى المتغيرات الخارجية ولم تكن تمتلك الخاصية المذكورة في الأعلي؟
9091

91-
What if it could access the outer variables?
92+
المشكلة هي أن قبل أن يتم نشر مشروع الجافاسكريبت الخاص بك للإنتاج. يتم ضغطه عن طريق إستخدام شئ يسمي -- *minifier* -- يعتبر هذا البرنامج خاص بتقليص حجم الكود الخاص بك عن طريق مسح الزيادات مثل التعليقات و المسافات و الأهم من ذلك أنه يغير أسماء المتغيرات المحلية الطويلة إلى أسماء متغيرات أقصر.
9293

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* يعتبر ذكي لأنه يحلل تركيب الكود لكي لايعطل شئ وليس فقط القيام بالتبديل.
9495

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` وتغيره.
9697

97-
So if `new Function` had access to outer variables, it would be unable to find renamed `userName`.
98+
إذا استطاعت الدالة الجديدة `new Function` الوصول للمتغبرات الخارجية ستكون هذه مشكلة كبير قد تعطل البرنامج لهذا السبب هي زُودت بخاصية عدم الوصول إلى المتغيرات الخارجية الذي ذكرناه بالأعلي
9899

99-
**If `new Function` had access to outer variables, it would have problems with minifiers.**
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.
104103

105-
## Summary
104+
## الملخص
106105

107-
The syntax:
106+
طريقة تركيب الجملة:
108107

109108
```js
110109
let func = new Function ([arg1, arg2, ...argN], functionBody);
111110
```
112111

113-
For historical reasons, arguments can also be given as a comma-separated list.
112+
لأسباب تاريخية, العوامل يمكن أن نرسلها علي شكل قائمة مفصولة بفاصلات.
113+
114+
لاحظ أن الثلاث توضيحات القادة كلهم يحملون نفس المعني:
114115

115-
These three declarations mean the same:
116116

117117
```js
118118
new Function('a', 'b', 'return a + b'); // basic syntax
119119
new Function('a,b', 'return a + b'); // comma-separated
120120
new Function('a , b', 'return a + b'); // comma-separated with spaces
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]]` التي تشير إلى الحدس الشامل "global Lexical Environment", وليس الحدس الخارجي. ولذلك هذه الدالة لاتستطيع الوصول للمتغيرات الخارجية ولكن هذا شئ جيد لانه لولا حدوثه ستحدث أخطاء كثيرة جداً عند تقليص الكود. وعند الحاجة إلى متغير خارجي نرسله علي شكل عامل للدالة ذلك يمنع كل المشاكل التي ذكرناها في الأعلي.
124+

0 commit comments

Comments
 (0)