|
1 | | -# The modern mode, "use strict" |
2 | | - |
3 | | -For a long time, JavaScript evolved without compatibility issues. New features were added to the language while old functionality didn't change. |
4 | | - |
5 | | -That had the benefit of never breaking existing code. But the downside was that any mistake or an imperfect decision made by JavaScript's creators got stuck in the language forever. |
6 | | - |
7 | | -This was the case until 2009 when ECMAScript 5 (ES5) appeared. It added new features to the language and modified some of the existing ones. To keep the old code working, most such modifications are off by default. You need to explicitly enable them with a special directive: `"use strict"`. |
8 | | - |
9 | | -## "use strict" |
10 | | - |
11 | | -The directive looks like a string: `"use strict"` or `'use strict'`. When it is located at the top of a script, the whole script works the "modern" way. |
12 | | - |
13 | | -For example: |
14 | | - |
15 | | -```js |
16 | | -"use strict"; |
17 | | - |
18 | | -// this code works the modern way |
19 | | -... |
| 1 | +# الوضع الصارم: النمط الحديث لكتابة الشيفرات |
| 2 | +تطورت لغة JavaScript خلال فترة طويلة دون أي مشاكل في التوافق. وجرى التحقق من استمرار عمل جميع وظائفها في كل مرة تضاف إليها خصائص جديدة. |
| 3 | + |
| 4 | +وكان لذلك فائدة كبيرة في استمرار عمل الشيفرات البرمجية الموجودة دون تعطلها. ولكن كان له ناحية سلبية أيضًا، هي أنّ الأخطاء والخصائص التي أضافها مطورو اللغة بقيت مع اللغة إلى الأبد. |
| 5 | + |
| 6 | +واستمرت هذا الحال حتى ظهور ECMAScript 5 (اختصارًا ES5) في عام 2009. التي أضافت خصائص جديدة إلى اللغة وعدّلت على بعض الخصائص الموجودة سابقًا. ولضمان استمرار عمل الشيفرات البرمجية السابقة، فإنّ معظم هذه التعديلات كانت غير فعّالة بشكل افتراضي؛ ولاستخدامها، عليك تفعيلها بشكل صريح باستخدام الموجه الخاص: `"use strict"`. |
| 7 | + |
| 8 | +## الموجه `"use strict"` |
| 9 | +يبدو الموجه كسلسلة نصية: `"use-strict"` أو `'use strict'`. وعندما يوضع في بداية السكربت، يعمل السكربت حسب النمط والطريقة الحالية. |
| 10 | + |
| 11 | +إليك المثال التالي: |
| 12 | + |
20 | 13 | ``` |
21 | | - |
22 | | -Quite soon we're going to learn functions (a way to group commands), so let's note in advance that `"use strict"` can be put at the beginning of a function. Doing that enables strict mode in that function only. But usually people use it for the whole script. |
23 | | - |
24 | | -````warn header="Ensure that \"use strict\" is at the top" |
25 | | -Please make sure that `"use strict"` is at the top of your scripts, otherwise strict mode may not be enabled. |
26 | | -
|
27 | | -Strict mode isn't enabled here: |
28 | | -
|
29 | | -```js no-strict |
30 | | -alert("some code"); |
31 | | -// "use strict" below is ignored--it must be at the top |
32 | | -
|
33 | 14 | "use strict"; |
34 | | -
|
35 | | -// strict mode is not activated |
| 15 | +//هذه الشيفرة البرمجية تعمل في الوضع الصارم |
| 16 | +… |
36 | 17 | ``` |
37 | | -
|
38 | | -Only comments may appear above `"use strict"`. |
39 | | -```` |
40 | | - |
41 | | -```warn header="There's no way to cancel `use strict`" |
42 | | -There is no directive like `"no use strict"` that reverts the engine to old behavior. |
43 | | - |
44 | | -Once we enter strict mode, there's no going back. |
| 18 | + |
| 19 | +قريبًا، سندرس الدوال (طريقة لجمع الأوامر). ولكن في نطرة استباقية للموضوع، لاحظ أنه بالإمكان وضع `"use strict"` في بداية معظم أنواع الدوال عوضًا عن كامل السكربت. وبذلك يتم تفعيل الوضع الصارم (strict mode) ضمن الدوال فقط. لكن عادةً يستخدم المبرمجون هذا الوضع لكامل السكربت. |
| 20 | + |
| 21 | +تأكد من وضع `"use script"` في بداية السكربتات، وإلا فإنه من الممكن ألا يُفعَّل الوضع الصارم. |
| 22 | +فمثلًا، الوضع الصارم غير مفعّل في المثال التالي: |
| 23 | + |
45 | 24 | ``` |
46 | | -
|
47 | | -## Browser console |
48 | | -
|
49 | | -When you use a [developer console](info:devtools) to run code, please note that it doesn't `use strict` by default. |
50 | | -
|
51 | | -Sometimes, when `use strict` makes a difference, you'll get incorrect results. |
52 | | -
|
53 | | -So, how to actually `use strict` in the console? |
54 | | -
|
55 | | -First, you can try to press `key:Shift+Enter` to input multiple lines, and put `use strict` on top, like this: |
56 | | -
|
57 | | -```js |
58 | | -'use strict'; <Shift+Enter for a newline> |
59 | | -// ...your code |
60 | | -<Enter to run> |
| 25 | +alert("some code"); |
| 26 | +// إن لم يُستعمَل في أول سطر "use strict" سيجري تجاهل الموجه |
| 27 | +"use strict" |
| 28 | +// الوضع الصارم غير مُفعَّل |
| 29 | +``` |
| 30 | + |
| 31 | +بإمكانك كتابة التعليقات فقط قبل استخدام الموجه `"use strict"`. |
| 32 | +لاحظ أيضًا أنه ليس هناك طريقة لإلغاء `"use strict"`، أي لا يوجد موجه آخر مثل `"no use strict"` والذي يثني المحرك عن عمله ويلغي تفعيل الوضع الصارم. لذلك عند بدء استخدام الوضع الصارم، لا توجد طريقة لإلغائه والعودة إلى الوضع الافتراضي. |
| 33 | + |
| 34 | +## طرفية المتصفح |
| 35 | +أبقِ في ذهنك أن طرفية المتصفح (console) لا تستخدم الوضع الصارم لاختبار وتنفيذ الشيفرات المكتوبة فيها؛ أي أنها لا تستخدم `"use strict"` افتراضيًا. |
| 36 | + |
| 37 | +عندما يوجد هناك اختلاف بين `"use strict"` والنمط الافتراضي في بعض الأحيان، قد تحصل على ناتج خطأ. ولن تنجح محاولتك في تفعيل الوضع الصارم (strict) بالضغط على الاختصار Shift+Enter لإدخال عدة أسطر، ثم استخدام `"use strict"` في البداية وذلك بسبب طريقة معالجة الطرفية للشيفرة البرمجية داخليًا. |
| 38 | + |
| 39 | +الطريقة الأفضل للتأكد من عمل `"use strict"` هو كتابة الشيفرة البرمجية ضمن الطرفية كما يلي: |
| 40 | + |
61 | 41 | ``` |
62 | | - |
63 | | -It works in most browsers, namely Firefox and Chrome. |
64 | | - |
65 | | -If it doesn't, e.g. in an old browser, there's an ugly, but reliable way to ensure `use strict`. Put it inside this kind of wrapper: |
66 | | - |
67 | | -```js |
68 | 42 | (function() { |
69 | | - 'use strict'; |
70 | | - |
71 | | - // ...your code here... |
| 43 | +'use strict'; |
| 44 | +// … ضع شيفرتك هنا … |
72 | 45 | })() |
73 | 46 | ``` |
74 | | - |
75 | | -## Should we "use strict"? |
76 | | - |
77 | | -The question may sound obvious, but it's not so. |
78 | | - |
79 | | -One could recommend to start scripts with `"use strict"`... But you know what's cool? |
80 | | - |
81 | | -Modern JavaScript supports "classes" and "modules" - advanced language structures (we'll surely get to them), that enable `use strict` automatically. So we don't need to add the `"use strict"` directive, if we use them. |
82 | | - |
83 | | -**So, for now `"use strict";` is a welcome guest at the top of your scripts. Later, when your code is all in classes and modules, you may omit it.** |
84 | | - |
85 | | -As of now, we've got to know about `use strict` in general. |
86 | | - |
87 | | -In the next chapters, as we learn language features, we'll see the differences between the strict and old modes. Luckily, there aren't many and they actually make our lives better. |
88 | | - |
89 | | -All examples in this tutorial assume strict mode unless (very rarely) specified otherwise. |
| 47 | + |
| 48 | +## استخدم "use strict" دومًا |
| 49 | +سنتعلم لاحقًا الاختلافات بين الوضع الصارم والوضع الافتراضي الذي تعمل ضمنه الشيفرات. ففي الفصل القادم، ستلاحظ الاختلافات بين هذين الوضعين أثناء تعلمك للخصائص الجديدة. من الجيد أنه لا يوجد الكثير من هذه الاختلافات، وفي حال وجودها فهي لتحسين عمل الشيفرة البرمجية. ولكن في الوقت الحالي تكفي معرفتك بهذه الأمور العامة: |
| 50 | + |
| 51 | +1. الموجه `"use strict"` يحول المحرك إلى النمط الحديث (modern)، مما يغير من طريقة تعامله مع بعض الخصائص الموجودة سابقًا. سندرس ذلك بالتفصيل في الأجزاء القادمة من هذه السلسلة التعليمية. |
| 52 | +2. يتم تفعيل الوضع الصارم بوضع الموجه `"use strict"` في بداية السكربت أو الدالة. العديد من خصائص اللغة، مثل الأصناف (classes) والوحدات (modules)، ُتفعِّل الوضع الصارم (strict) تلقائيًا. |
| 53 | +3. الوضع الصارم (strict) مدعوم من قبل جميع المتصفحات الحالية. |
| 54 | +4. أنصحك دائمًا بوضع `"use strict"` في بداية السكربت. جميع الأمثلة في هذه السلسلة التعليمية تفترض العمل ضمن الوضع الصارم إلا إذا ذُكر غير ذلك وهذا في بعض الحالات النادرة. |
| 55 | + |
| 56 | +ترجمة -وبتصرف- للفصل [Modern mode, "use strict"](https://javascript.info/use-strict) من كتاب [The JavaScript Language](https://javascript.info/js) |
0 commit comments