Skip to content

Commit d975421

Browse files
authored
Merge pull request #102 from Adham-Niazy/master
Translate Old Var to Ar
2 parents f141905 + 955bf73 commit d975421

1 file changed

Lines changed: 97 additions & 78 deletions

File tree

1-js/06-advanced-functions/04-var/article.md

Lines changed: 97 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11

2-
# The old "var"
2+
# إفادة «var» القديمة
33

4-
```smart header="This article is for understanding old scripts"
5-
The information in this article is useful for understanding old scripts.
4+
```smart header="هذه المقالة من أجل فهم النصوص القديمة"
5+
المعلومات داخل هذه المقالة تساعدنا فى فهم النصوص القديمة أكثر ولا تحتوي علي أي معلومات عن كيفية كتابة كود جديد
66
7-
That's not how we write a new code.
87
```
98

10-
In the very first chapter about [variables](info:variables), we mentioned three ways of variable declaration:
9+
ذكرنا في أوائل الفصول حين تكلمنا عن [المتغيرات](info:variables), ذكرنا ثلاث طرائق للتصريح عنها:
1110

1211
1. `let`
1312
2. `const`
1413
3. `var`
1514

15+
16+
تصرّف كلا الإفادتين `‎let‎` و`‎const‎` بذات الطريقة (بالمقايسة مع البيئات المُعجمية).
17+
18+
بينما `‎var‎` فهو وحش آخر مختلف جذريًا ويعود في أصله إلى قرون سحيقة. لا نستعمله عادةً في السكربتات الحديثة ولكنّك ستجده حتمًا خلف إحدى صخور السكربتات القديمة.
19+
20+
لو لم ترغب بالتعرّف على هذه السكربتات فيمكنك تخطّي هذا الفصل أو تأجيله لوقت لاحق. ولكن لا تنسَ احتمالية ندمك لاحقًا فيغدر بك هذا الوحش.
21+
22+
من أول وهلة نرى بأنّ تصرّف `‎var‎` يشابه تصرّف `‎let‎`، أي أنّه يُصرّح (مثل الثاني) عن متغير:
23+
24+
25+
```js run
26+
function sayHi() {
27+
var phrase = "Hello"; // ‫متغير محلي، استعملنا «var» بدل «let»
28+
29+
alert(phrase); // Hello
30+
}
31+
=======
1632
The `var` declaration is similar to `let`. Most of the time we can replace `let` by `var` or vice-versa and expect things to work:
1733

1834
```js run
@@ -22,69 +38,79 @@ alert(message); // Hi
2238

2339
But internally `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
2440
41+
alert(phrase); // ‫خطأ، phrase غير معرّف
42+
43+
```
44+
45+
ولكن... ما خفي كان أعظم. إليك الفروق.
46+
=======
2547
If you don't plan on meeting such scripts you may even skip this chapter or postpone it.
2648

2749
On the other hand, it's important to understand differences when migrating old scripts from `var` to `let`, to avoid odd errors.
2850
2951
## "var" has no block scope
3052
31-
Variables, declared with `var`, are either function-wide or global. They are visible through blocks.
53+
حين نصرّح عن المتغيرات باستعمال `‎var‎` نكون جعلناها معروفة للدالة كاملةً (لو كانت في دالة) أو عمومية في السكربت. يمكنك أن ترى تلك المتغيرات إن اخترقت «جدران» الكُتل.
54+
55+
مثال:
3256
33-
For instance:
3457
3558
```js run
3659
if (true) {
37-
var test = true; // use "var" instead of "let"
60+
var test = true; // ‫نستعمل «var» بدل «let»
3861
}
3962
40-
*!*
41-
alert(test); // true, the variable lives after if
42-
*/!*
63+
alert(test); // ‫الناتج true، أي أنّ المتغير «حيّ يُرزق» بعد إفادة if
64+
4365
```
4466
45-
As `var` ignores code blocks, we've got a global variable `test`.
4667
47-
If we used `let test` instead of `var test`, then the variable would only be visible inside `if`:
68+
تجاهل `‎var‎` كتل الشيفرة، وبهذا صار متغير `‎test‎` عموميًا.
69+
70+
لو استعملنا `‎let test‎` بدل `‎var test‎` فسيكون المتغير ظاهرًا لباقي الشيفرة داخل إفادة `‎if‎` فقط لا غير:
71+
4872
4973
```js run
5074
if (true) {
51-
let test = true; // use "let"
75+
let test = true; // ‫نستعمل «let»
5276
}
5377
54-
*!*
55-
alert(test); // Error: test is not defined
56-
*/!*
57-
```
78+
alert(test); // ‫خطأ: لم يُعرّف عن test
5879
59-
The same thing for loops: `var` cannot be block- or loop-local:
80+
```
81+
يسري الأمر ذاته على الحلقات فلا يمكن أن يكون `‎var‎` محليًا حسب الكتلة أو حسب الحلقة:
6082
6183
```js
6284
for (var i = 0; i < 10; i++) {
6385
// ...
6486
}
6587
66-
*!*
67-
alert(i); // 10, "i" is visible after loop, it's a global variable
68-
*/!*
88+
alert(i); // ‫10، ظهر «i» بعد الحلقة فهو متغير عمومي
89+
6990
```
7091
71-
If a code block is inside a function, then `var` becomes a function-level variable:
92+
لو كتبت كتلة شيفرة في دالة فسيصير `‎var‎` متغيرًا على مستوى الدالة كاملةً.
7293
7394
```js run
7495
function sayHi() {
7596
if (true) {
7697
var phrase = "Hello";
7798
}
7899
79-
alert(phrase); // works
100+
alert(phrase); // يمكننا فعل هذا
80101
}
81102
82103
sayHi();
83-
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
104+
alert(phrase); // ‫خطأ: phrase غير معرّف (طالِع مِعراض المطوّر)
105+
84106
```
85107
86-
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript blocks had no Lexical Environments. And `var` is a remnant of that.
108+
كما نرى فإفادة `‎var‎` تخترق كُتل `‎if‎` و`‎for‎` وغيرها من كُتل شيفرة. يعزو ذلك إلى أنّه في الزمن الماضي الجميل لم تكن لكُتل جافاسكربت بيئات مُعجمية. و`‎var‎` إحدى آثار ذلك الزمن.
87109
110+
## تُعالج التصريحات باستعمال `‎var‎` عند بدء الدالة
111+
112+
ُعالج التصريحات باستعمال `‎var‎` متى ما بدأت الدالة (أو بدأ السكربت، للمتغيرات العمومية).
113+
=======
88114
## "var" tolerates redeclarations
89115
90116
If we declare the same variable with `let` twice in the same scope, that's an error:
@@ -107,111 +133,105 @@ alert(user); // John
107133

108134
## "var" variables can be declared below their use
109135

110-
`var` declarations are processed when the function starts (or script starts for globals).
136+
أي أنّ متغيرات `var` تُعرّف من بداية الدالة مهما كان مكان تعريفها (هذا لو لم يكن التعريف في دالة متداخلة أخرى).
111137

112-
In other words, `var` variables are defined from the beginning of the function, no matter where the definition is (assuming that the definition is not in the nested function).
138+
يعني ذلك أنّ هذه الشيفرة:
113139

114-
So this code:
115140

116141
```js run
117142
function sayHi() {
118143
phrase = "Hello";
119144

120145
alert(phrase);
121146

122-
*!*
123147
var phrase;
124-
*/!*
125148
}
126149
sayHi();
127-
```
128150

129-
...Is technically the same as this (moved `var phrase` above):
151+
```
152+
متطابقة تقنيًا مع هذه (بتحريك `‎var phrase‎` إلى أعلى):
130153

131154
```js run
132155
function sayHi() {
133-
*!*
134156
var phrase;
135-
*/!*
136157

137158
phrase = "Hello";
138159

139160
alert(phrase);
140161
}
141162
sayHi();
142-
```
143163

144-
...Or even as this (remember, code blocks are ignored):
164+
```
145165

166+
أو حتى هذه (لا تنسَ بأنّ كُتل الشيفرات مُهملة):
146167
```js run
147168
function sayHi() {
148169
phrase = "Hello"; // (*)
149170

150-
*!*
151171
if (false) {
152172
var phrase;
153173
}
154-
*/!*
155174

156175
alert(phrase);
157176
}
158177
sayHi();
178+
159179
```
180+
يدعو الناس هذا السلوك بسلوك «الطفو» _hoisting_ (أو الرفع) إذ أنّ متغيرات `‎var‎` «تطفو» إلى أعلى الدالة (أو ترتفع إلى أعلاها).
160181

161-
People also call such behavior "hoisting" (raising), because all `var` are "hoisted" (raised) to the top of the function.
182+
أي أنّه في المثال أعلاه، الفرع `‎if (false)‎` من الإفادة لا يعمل قط ولكن هذا ليس بمهم، إذ أنّ `var` داخله سيُعالج في بداية الدالة، وحين تصل عملية التنفيذ إلى `‎(*)‎` سيكون المتغير موجودًا لا محالة.
162183

163-
So in the example above, `if (false)` branch never executes, but that doesn't matter. The `var` inside it is processed in the beginning of the function, so at the moment of `(*)` the variable exists.
184+
**التصريحات تطفو صحيح، ولكنّ ليس عبارات الإسناد.**
164185

165-
**Declarations are hoisted, but assignments are not.**
186+
الأفضل لو نمثّل ذلك في هذا المثال:
166187

167-
That's best demonstrated with an example:
168188

169189
```js run
170190
function sayHi() {
171191
alert(phrase);
172192

173-
*!*
174193
var phrase = "Hello";
175-
*/!*
176194
}
177195

178196
sayHi();
197+
179198
```
180199

181-
The line `var phrase = "Hello"` has two actions in it:
200+
في السطر `‎var phrase = "Hello"‎` إجراءان اثنان:
201+
202+
1. التصريح عن المتغير باستعمال `var`
203+
2. إسناد قيمة للمتغير باستعمال `‎=‎`.
182204

183-
1. Variable declaration `var`
184-
2. Variable assignment `=`.
205+
يتعامل المحرّك مع التصريحات متى بدء تنفيذ الدالة (إذ التصريحات تطفو)، ولكنّ عبارة الإسناد لا تعمل إلّا حيثما ظهرت، فقط. إذًا فالشيفرة تعمل بهذا النحو فعليًا:
185206

186-
The declaration is processed at the start of function execution ("hoisted"), but the assignment always works at the place where it appears. So the code works essentially like this:
187207

188208
```js run
189209
function sayHi() {
190-
*!*
191-
var phrase; // declaration works at the start...
192-
*/!*
210+
var phrase; // ‫بادئ ذي بدء، يعمل التصريح...
193211

194-
alert(phrase); // undefined
212+
alert(phrase); // غير معرّف
195213

196-
*!*
197-
phrase = "Hello"; // ...assignment - when the execution reaches it.
198-
*/!*
214+
phrase = "Hello"; // ‫...هنا.
199215
}
200216

201217
sayHi();
218+
202219
```
203220

204-
Because all `var` declarations are processed at the function start, we can reference them at any place. But variables are undefined until the assignments.
221+
يُعالج المحرّك التصريحات `‎var‎` حين تبدأ الدوال، وبهذا يمكننا الإشارة إليها أينما أردنا في الشيفرة. ولكن انتبه فالمتغيرات غير معرّفة حتى تُسند إليها قيم.
222+
223+
في الأمثلة أعلاه عمل التابِع `‎alert‎` دون أيّ أخطاء إذ أن المتغير `‎phrase‎` موجود. ولكن لم تُسند فيه قيمة بعد فعرض `‎undefined‎`.
205224

206-
In both examples above `alert` runs without an error, because the variable `phrase` exists. But its value is not yet assigned, so it shows `undefined`.
207225

208-
### IIFE
226+
### تعريف الدالة المناداة تواً (IIFE)
209227

210-
As in the past there was only `var`, and it has no block-level visibility, programmers invented a way to emulate it. What they did was called "immediately-invoked function expressions" (abbreviated as IIFE).
228+
في الماضي كان هناك فقط `var`, وليس له مستوي كتلة, لكن المبرمجين إخترعوا طريقة لحل ذلك. التي تسمي
211229

212-
That's not something we should use nowadays, but you can find them in old scripts.
230+
"**I**mmediately-**I**nvoked **f**unction **E**xpressions"
213231

214-
An IIFE looks like this:
232+
هذا لا يستخدم حالياً, لكن تستطيع إيجادهم في النصوص القديمة.
233+
234+
طريقة كتابة (IIFE):
215235

216236
```js run
217237
(function() {
@@ -222,13 +242,13 @@ An IIFE looks like this:
222242

223243
})();
224244
```
245+
هذا يعتبر تعريف دالة صُنعت وتمت مناداتها على الفور. لذلك يتم تنفيذ الكود ولها متغيراتها الخاصة.
225246

226-
Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables.
247+
يتم تغليف تعريف الدالة يتم تغليفه بداخل قوسين (function {...})`, لأن عندما تقابل جافاسكريبت `"function"` في الكود الأساسي, تفهمها علي أنها بداية تعريف دالة ولكن بدون إسم لذلك يعطينا خطأ:
227248

228-
The Function Expression is wrapped with parenthesis `(function {...})`, because when JavaScript meets `"function"` in the main code flow, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so this kind of code will give an error:
229249

230250
```js run
231-
// Try to declare and immediately call a function
251+
// محاولة صنع دالة فورية التنفيذ
232252
function() { // <-- Error: Function statements require a function name
233253

234254
let message = "Hello";
@@ -237,19 +257,19 @@ function() { // <-- Error: Function statements require a function name
237257

238258
}();
239259
```
240-
241-
Even if we say: "okay, let's add a name", that won't work, as JavaScript does not allow Function Declarations to be called immediately:
260+
حتي لو وضعنا إسم, لن يعمل هذا, لأن جافاسكريبت لا تسمح بتعريف دالة ستتم مناداتها فورياً:
242261

243262
```js run
244-
// syntax error because of parentheses below
263+
// هذا خطأ بسبب الأقواس الموجودة بالأسفل
245264
function go() {
246265

247-
}(); // <-- can't call Function Declaration immediately
266+
}();
248267
```
249268

250-
So, the parentheses around the function is a trick to show JavaScript that the function is created in the context of another expression, and hence it's a Function Expression: it needs no name and can be called immediately.
269+
لذلك, الأقواس حول الدالة تعتبر خدعة لجعل جافاسكريبت فهم أننا نصنع سياق أخر للتعريف, لذلك إن تعريف الدالة: لا يحتاج إلي اسم لذلك تتم مناداتها علي الفور
270+
251271

252-
There exist other ways besides parentheses to tell JavaScript that we mean a Function Expression:
272+
هناك أيضاً طرق أخرى:
253273

254274
```js run
255275
// Ways to create IIFE
@@ -271,15 +291,14 @@ There exist other ways besides parentheses to tell JavaScript that we mean a Fun
271291
}();
272292
```
273293

274-
In all the above cases we declare a Function Expression and run it immediately. Let's note again: nowadays there's no reason to write such code.
294+
## ملخص
275295

276-
## Summary
296+
هناك فرقين جوهرين بين `‎var‎` موازنةً بِـ `‎let/const‎`:
277297

278-
There are two main differences of `var` compared to `let/const`:
298+
1. ليس لمتغيرات `‎var‎` نطاقًا كتليًا وأصغر نطاق لها هو في الدوال.
299+
2. تُعالج التصريحات باستعمال `‎var‎` عند بدء الدالة (أو بدء السكربت، للمتغيرات العمومية).
279300

280-
1. `var` variables have no block scope, they are visible minimum at the function level.
281-
2. `var` declarations are processed at function start (script start for globals).
301+
هناك فرق آخر صغير يتعلّق بالكائن العمومي وسنشرحه في الفصل التالي.
282302

283-
There's one more very minor difference related to the global object, that we'll cover in the next chapter.
303+
بهذا، غالبًا ما يكون استعمال `‎var‎` أسوأ بكثير من `‎let‎` بعدما عرفت الفروق بينها، فالمتغيرات على مستوى الكُتل أمر رائع جدًا ولهذا السبب تمامًا أُضيفت `‎let‎` إلى معيار اللغة منذ زمن وصارت الآن الطريقة الأساسية (هي و`‎const‎`) للتصريح عن متغير.
284304

285-
These differences make `var` worse than `let` most of the time. Block-level variables is such a great thing. That's why `let` was introduced in the standard long ago, and is now a major way (along with `const`) to declare a variable.

0 commit comments

Comments
 (0)