Skip to content

Commit 486f921

Browse files
committed
currying and eval in miscellaneous chapter
1 parent 1009eaf commit 486f921

2 files changed

Lines changed: 143 additions & 123 deletions

File tree

1-js/99-js-misc/02-eval/article.md

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,141 @@
1-
# Eval: run a code string
1+
# الدالّة "Eval" لتنفيذ الشيفرة البرمجية
22

3-
The built-in `eval` function allows to execute a string of code.
3+
تنفذ الدالّة `Eval` المضمّنة في اللغة الشيفرات البرمجية المُمرّرة لها كسلسلة نصية `string`.
44

5-
The syntax is:
5+
وصياغتها هكذا:
66

7-
```js
7+
```
88
let result = eval(code);
99
```
1010

11-
For example:
11+
فمثلًا:
1212

13-
```js run
13+
```
1414
let code = 'alert("Hello")';
1515
eval(code); // Hello
1616
```
1717

18-
A string of code may be long, contain line breaks, function declarations, variables and so on.
18+
يمكن أن تكون الشيفرة المُمررة للدالّة كبيرة وتحتوي على فواصل أسطر وتعريف دوالّ ومتغيّرات، وما إلى ذلك.
19+
20+
ولكن نتيجة الدالّة `Eval` هي نتيجة أخر عبارة منفذة في الشيفرة.
21+
22+
وإليك المثال التالي:
1923

20-
The result of `eval` is the result of the last statement.
2124

22-
For example:
23-
```js run
25+
```
2426
let value = eval('1+1');
2527
alert(value); // 2
2628
```
2729

28-
```js run
30+
```
2931
let value = eval('let i = 0; ++i');
3032
alert(value); // 1
3133
```
3234

33-
The eval'ed code is executed in the current lexical environment, so it can see outer variables:
35+
تُنفذّ الشيفرة في البيئة الحالية للدالّة، ولذا فيمكنها رؤية المتغيرات الخارجية:
3436

35-
```js run no-beautify
37+
```
3638
let a = 1;
3739
3840
function f() {
3941
let a = 2;
4042
41-
*!*
4243
eval('alert(a)'); // 2
43-
*/!*
4444
}
4545
4646
f();
4747
```
4848

49-
It can change outer variables as well:
49+
كما يمكنها تعديل المتغيّرات الخارجية أيضًا:
5050

51-
```js untrusted refresh run
51+
```
5252
let x = 5;
5353
eval("x = 10");
54-
alert(x); // 10, value modified
54+
alert(x); // النتيجة: ‫10، تعدلت القيمة بنجاح
5555
```
5656

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` بيئة متغيّرات خاصة بها. لذا فلن تظهر الدوالّ والمتغيرات، المعرفة -داخل الدالة- للخارج وإنما ستبقى بداخلها:
6158

59+
```
60+
// تذكر أن في الوضع الصارم يُشغّلُ تلقائيًا في الأمثلة الحيّة
6261
eval("let x = 5; function f() {}");
6362
64-
alert(typeof x); // undefined (no such variable)
65-
// function f is also not visible
63+
alert(typeof x); // undefined (المتحول غير مرئي هنا)
64+
// ‫الدالّة f غير مرئية هنا أيضًا
6665
```
66+
بدون تفعيل "الوضع صارم"، لن يكون للدالّة `Eval` بيئة متغيرات خاصة بها، ولذلك سنرى المتغيّر `x` والدالّة `f` من خارج الدالّة.
6767

68-
Without `use strict`, `eval` doesn't have its own lexical environment, so we would see `x` and `f` outside.
68+
## استخدامات الدالّة "Eval"
6969

70-
## Using "eval"
70+
في طرق البرمجة الحديثة، نادرًا ما تستخدم الدالّة `Eval`. وغالبًا ما يقال عنها أنها أصل الشرور.
7171

72-
In modern programming `eval` is used very sparingly. It's often said that "eval is evil".
72+
والسبب بسيط: إذ كانت لغة جافاسكربت منذ زمن بعيد أضعف بكثير من الآن، ولم يكُ بالإمكان فعل إيّ شيء إلا باستخدام الدالّة `Eval`. ولكن ذلك الوقت مضى عليه عقد من الزمن.
7373

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+
حاليًا، لا يوجد سبب وجيه لاستخدامها. ولو أن شخصًا يستخدمها الآن فلديه الإمكانية لاستبدالها بالبنية الحديثة للغة أو [بالوحدات]().
7575

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` للمتغيرات الخارجية لها عواقب سيئة.
7777

78-
Please note that its ability to access outer variables has side-effects.
78+
إن عملية تصغير الشيفرة (هي الأدوات تستخدم لتصغير شيفرة الجافاسكربت قبل نشرها وذلك لتصغير حجمها أكثر من ذي قبل) تعيد تسمية المتغيّرات المحلية لأسماء أقصر (مثل `a` و`b` وما إلى ذلك) لتصغير الشيفرة. وعادةً ما تكون هذه العلمية آمنة، ولكن ليس في حال استخدام الدالّة `Eval`، إذ يمكننا الوصول للمتغيّرات المحلية من الشيفرة المُمررة للدالّة. لذا، لن تصغّر المتغيرات التي يحتمل أن تكون مرئية من الدالة `Eval`. مما سيُؤثر سلبًا على نسبة ضغط الشيفرة.
7979

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` من الممارسات البرمجية السيئة، لأنه يزيد صعوبة صيانة الشيفرة.
8181

82-
Using outer local variables inside `eval` is also considered a bad programming practice, as it makes maintaining the code more difficult.
82+
هناك طريقتان لضمان الأمان الكامل عند مصادفتك مثل هذه المشاكل.
8383

84-
There are two ways how to be totally safe from such problems.
84+
**إذا لم تستخدم الشيفرة الممررة للدالّة المتغيرات الخارجية، فمن الأفضل استدعاء الدالّة هكذا: `window.eval(...)‎`**
8585

86-
**If eval'ed code doesn't use outer variables, please call `eval` as `window.eval(...)`:**
86+
بهذه الطريقة ستُنفذّ الشيفرة في النطاق العام:
8787

88-
This way the code is executed in the global scope:
89-
90-
```js untrusted refresh run
88+
```
9189
let x = 1;
9290
{
9391
let x = 5;
9492
window.eval('alert(x)'); // 1 (global variable)
9593
}
9694
```
9795

98-
**If eval'ed code needs local variables, change `eval` to `new Function` and pass them as arguments:**
96+
**إن احتاجت الشيفرة الممررة للدالة `Eval` لمتغيّرات خارجية، فغيّر `Eval` لتصبح `new Function` ومرّر المتغير كوسيط. هكذا:**
9997

100-
```js run
98+
```
10199
let f = new Function('a', 'alert(a)');
102100
103101
f(5); // 5
104102
```
105103

106-
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)، في النطاق العام. لذا لن تتمكن من رؤية المتغيرات المحلية. ولكن من الواضح أن تمريرها المتغيرات صراحة كوسطاء سيحلّ المشكلة، كما رأينا في المثال أعلاه.
105+
106+
## خلاصة
107+
108+
سيُشغّل استدعاء الدالّة `eval(code)‎` الشيفرة البرمجية المُمرّرة ويعيد نتيجة العبارة الأخيرة.
109+
110+
- نادرًا ما تستخدم هذه الدالّة في الإصدارات الحديثة للغة، إذ لا توجد حاجة ماسّة لها.
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+
يستطيع المستخدم أيضًا إدخال أي نص أو شيفرة.
107138

108-
## Summary
139+
لجعل الشيفرة آمنة، وحصرها للعمليات الرياضية فحسب، سنتحقق من `expr` باستخدام [التعابير النمطية](https://javascript.info/regular-expressions) ، لكي لا تحتوي إلا على الأرقام والمعاملات رياضية.
109140

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

Comments
 (0)