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
The built-in `eval` function allows to execute a string of code.
3
+
تنفذ الدالّة `Eval` المضمّنة في اللغة الشيفرات البرمجية المُمرّرة لها كسلسلة نصية `string`.
4
4
5
-
The syntax is:
5
+
وصياغتها هكذا:
6
6
7
-
```js
7
+
```
8
8
let result = eval(code);
9
9
```
10
10
11
-
For example:
11
+
فمثلًا:
12
12
13
-
```js run
13
+
```
14
14
let code = 'alert("Hello")';
15
15
eval(code); // Hello
16
16
```
17
17
18
-
A string of code may be long, contain line breaks, function declarations, variables and so on.
18
+
يمكن أن تكون الشيفرة المُمررة للدالّة كبيرة وتحتوي على فواصل أسطر وتعريف دوالّ ومتغيّرات، وما إلى ذلك.
19
+
20
+
ولكن نتيجة الدالّة `Eval` هي نتيجة أخر عبارة منفذة في الشيفرة.
21
+
22
+
وإليك المثال التالي:
19
23
20
-
The result of `eval` is the result of the last statement.
21
24
22
-
For example:
23
-
```js run
25
+
```
24
26
let value = eval('1+1');
25
27
alert(value); // 2
26
28
```
27
29
28
-
```js run
30
+
```
29
31
let value = eval('let i = 0; ++i');
30
32
alert(value); // 1
31
33
```
32
34
33
-
The eval'ed code is executed in the current lexical environment, so it can see outer variables:
35
+
تُنفذّ الشيفرة في البيئة الحالية للدالّة، ولذا فيمكنها رؤية المتغيرات الخارجية:
34
36
35
-
```js run no-beautify
37
+
```
36
38
let a = 1;
37
39
38
40
function f() {
39
41
let a = 2;
40
42
41
-
*!*
42
43
eval('alert(a)'); // 2
43
-
*/!*
44
44
}
45
45
46
46
f();
47
47
```
48
48
49
-
It can change outer variables as well:
49
+
كما يمكنها تعديل المتغيّرات الخارجية أيضًا:
50
50
51
-
```js untrusted refresh run
51
+
```
52
52
let x = 5;
53
53
eval("x = 10");
54
-
alert(x); //10, value modified
54
+
alert(x); // النتيجة: 10، تعدلت القيمة بنجاح
55
55
```
56
56
57
-
In strict mode, `eval` has its own lexical environment. So functions and variables, declared inside eval, are not visible outside:
58
-
59
-
```js untrusted refresh run
60
-
// reminder: 'use strict' is enabled in runnable examples by default
57
+
في الوضع الصارم، تملك الدالّة `Eval` بيئة متغيّرات خاصة بها. لذا فلن تظهر الدوالّ والمتغيرات، المعرفة -داخل الدالة- للخارج وإنما ستبقى بداخلها:
61
58
59
+
```
60
+
// تذكر أن في الوضع الصارم يُشغّلُ تلقائيًا في الأمثلة الحيّة
62
61
eval("let x = 5; function f() {}");
63
62
64
-
alert(typeof x); // undefined (no such variable)
65
-
//function f is also not visible
63
+
alert(typeof x); // undefined (المتحول غير مرئي هنا)
64
+
// الدالّة f غير مرئية هنا أيضًا
66
65
```
66
+
بدون تفعيل "الوضع صارم"، لن يكون للدالّة `Eval` بيئة متغيرات خاصة بها، ولذلك سنرى المتغيّر `x` والدالّة `f` من خارج الدالّة.
67
67
68
-
Without `use strict`, `eval` doesn't have its own lexical environment, so we would see `x` and `f` outside.
68
+
## استخدامات الدالّة "Eval"
69
69
70
-
## Using "eval"
70
+
في طرق البرمجة الحديثة، نادرًا ما تستخدم الدالّة `Eval`. وغالبًا ما يقال عنها أنها أصل الشرور.
71
71
72
-
In modern programming `eval` is used very sparingly. It's often said that "eval is evil".
72
+
والسبب بسيط: إذ كانت لغة جافاسكربت منذ زمن بعيد أضعف بكثير من الآن، ولم يكُ بالإمكان فعل إيّ شيء إلا باستخدام الدالّة `Eval`. ولكن ذلك الوقت مضى عليه عقد من الزمن.
73
73
74
-
The reason is simple: long, long time ago JavaScript was a much weaker language, many things could only be done with `eval`. But that time passed a decade ago.
74
+
حاليًا، لا يوجد سبب وجيه لاستخدامها. ولو أن شخصًا يستخدمها الآن فلديه الإمكانية لاستبدالها بالبنية الحديثة للغة أو [بالوحدات]().
75
75
76
-
Right now, there's almost no reason to use `eval`. If someone is using it, there's a good chance they can replace it with a modern language construct or a [JavaScript Module](info:modules).
76
+
لاحظ أن إمكانية وصول الدالة `eval` للمتغيرات الخارجية لها عواقب سيئة.
77
77
78
-
Please note that its ability to access outer variables has side-effects.
78
+
إن عملية تصغير الشيفرة (هي الأدوات تستخدم لتصغير شيفرة الجافاسكربت قبل نشرها وذلك لتصغير حجمها أكثر من ذي قبل) تعيد تسمية المتغيّرات المحلية لأسماء أقصر (مثل `a` و`b` وما إلى ذلك) لتصغير الشيفرة. وعادةً ما تكون هذه العلمية آمنة، ولكن ليس في حال استخدام الدالّة `Eval`، إذ يمكننا الوصول للمتغيّرات المحلية من الشيفرة المُمررة للدالّة. لذا، لن تصغّر المتغيرات التي يحتمل أن تكون مرئية من الدالة `Eval`. مما سيُؤثر سلبًا على نسبة ضغط الشيفرة.
79
79
80
-
Code minifiers (tools used before JS gets to production, to compress it) rename local variables into shorter ones (like `a`, `b` etc) to make the code smaller. That's usually safe, but not if `eval` is used, as local variables may be accessed from eval'ed code string. So minifiers don't do that renaming for all variables potentially visible from `eval`. That negatively affects code compression ratio.
80
+
يُعدّ استخدام المتغيّرات المحلية في الشيفرة بداخل الدالّة `Eval` من الممارسات البرمجية السيئة، لأنه يزيد صعوبة صيانة الشيفرة.
81
81
82
-
Using outer local variables inside `eval` is also considered a bad programming practice, as it makes maintaining the code more difficult.
82
+
هناك طريقتان لضمان الأمان الكامل عند مصادفتك مثل هذه المشاكل.
83
83
84
-
There are two ways how to be totally safe from such problems.
84
+
**إذا لم تستخدم الشيفرة الممررة للدالّة المتغيرات الخارجية، فمن الأفضل استدعاء الدالّة هكذا: `window.eval(...)`**
85
85
86
-
**If eval'ed code doesn't use outer variables, please call `eval` as `window.eval(...)`:**
86
+
بهذه الطريقة ستُنفذّ الشيفرة في النطاق العام:
87
87
88
-
This way the code is executed in the global scope:
89
-
90
-
```js untrusted refresh run
88
+
```
91
89
let x = 1;
92
90
{
93
91
let x = 5;
94
92
window.eval('alert(x)'); // 1 (global variable)
95
93
}
96
94
```
97
95
98
-
**If eval'ed code needs local variables, change `eval` to`new Function`and pass them as arguments:**
The `new Function` construct is explained in the chapter <info:new-function>. It creates a function from a string, also in the global scope. So it can't see local variables. But it's so much clearer to pass them explicitly as arguments, like in the example above.
104
+
شرحنا في مقالٍ سابق تعلمنا كيفية استخدام [صياغة «الدالة الجديدة» new Function](). إذ باستخدام هذه الصياغة ستُنشأ دالة جديدة من السلسلة (String)، في النطاق العام. لذا لن تتمكن من رؤية المتغيرات المحلية. ولكن من الواضح أن تمريرها المتغيرات صراحة كوسطاء سيحلّ المشكلة، كما رأينا في المثال أعلاه.
- نادرًا ما تستخدم هذه الدالّة في الإصدارات الحديثة للغة، إذ لا توجد حاجة ماسّة لها.
111
+
- يمكننا الوصول دائمًا للمتغيّرات الخارجية في الدالّة `eval`. ولكن يعدّ ذلك من الممارسات السيئة.
112
+
- بدلًا من ذلك يمكننا استخدام الدالة `eval` في النطاق العام، هكذا `window.eval(code)`.
113
+
- أو، إذا كانت الشيفرة الخاصة بك تحتاج لبعض البيانات من النطاق الخارجي، فاستخدم صياغة `الدالّة الجديدة` ومرّر لها المتغيرات كوسطاء.
114
+
115
+
## التمارين
116
+
117
+
### آلة حاسبة باستخدام الدالة Eval
118
+
119
+
_الأهمية: 4_
120
+
121
+
أنشئ آلة حاسبة تطالب بتعبير رياضي وتُعيد نتيجته.
122
+
123
+
لا داعي للتحقق من صحة التعبير في هذا التمرين. فقط قيّم التعبير وأعد نتيجته.
124
+
125
+
[لرؤية المثال الحي](https://javascript.info/eval#)
126
+
127
+
#### الحل
128
+
129
+
لنستخدم الدالة `eval` لحساب التعبير الرياضي:
130
+
131
+
```
132
+
let expr = prompt("Type an arithmetic expression?", '2*3+2');
133
+
134
+
alert( eval(expr) );
135
+
```
136
+
137
+
يستطيع المستخدم أيضًا إدخال أي نص أو شيفرة.
107
138
108
-
## Summary
139
+
لجعل الشيفرة آمنة، وحصرها للعمليات الرياضية فحسب، سنتحقق من `expr` باستخدام [التعابير النمطية](https://javascript.info/regular-expressions) ، لكي لا تحتوي إلا على الأرقام والمعاملات رياضية.
109
140
110
-
A call to `eval(code)` runs the string of code and returns the result of the last statement.
111
-
- Rarely used in modern JavaScript, as there's usually no need.
112
-
- Can access outer local variables. That's considered bad practice.
113
-
- Instead, to `eval` the code in the global scope, use `window.eval(code)`.
114
-
- Or, if your code needs some data from the outer scope, use `new Function` and pass it as arguments.
141
+
ترجمة -وبتصرف- للفصل [Eval: run a code string](https://javascript.info/eval) من كتاب [The JavaScript language](https://javascript.info/js)
0 commit comments