--- id: 56533eb9ac21ba0edf2244be title: Global Scope and Functions challengeType: 1 videoUrl: '' localeTitle: النطاق العالمي والوظائف --- ## Description
في JavaScript ، يشير النطاق إلى رؤية المتغيرات. المتغيرات التي يتم تعريفها خارج كتلة وظيفة لها نطاق عالمي . وهذا يعني أنه يمكن رؤيتها في أي مكان في شفرة جافا سكريبت. يتم إنشاء المتغيرات التي يتم استخدامها بدون الكلمة الأساسية var تلقائيًا في النطاق global . هذا يمكن أن يخلق عواقب غير مقصودة في مكان آخر في التعليمات البرمجية الخاصة بك أو عند تشغيل وظيفة مرة أخرى. يجب عليك دائما أن تعلن عن المتغيرات الخاصة بك مع var .
## Instructions
باستخدام var ، قم بتعريف متغير global myGlobal خارج أي وظيفة. قم بتهيئته بقيمة 10 . داخل الدالة fun1 ، قم بتعيين 5 إلى oopsGlobal دون استخدام الكلمة الأساسية var .
## Tests
```yml tests: - text: يجب تعريف myGlobal testString: 'assert(typeof myGlobal != "undefined", "myGlobal should be defined");' - text: يجب أن يكون ل myGlobal قيمة 10 testString: 'assert(myGlobal === 10, "myGlobal should have a value of 10");' - text: يجب إعلان myGlobal باستخدام الكلمة الرئيسية var testString: 'assert(/var\s+myGlobal/.test(code), "myGlobal should be declared using the var keyword");' - text: يجب أن يكون oopsGlobal متغيرًا عامًا وله قيمة 5 testString: 'assert(typeof oopsGlobal != "undefined" && oopsGlobal === 5, "oopsGlobal should be a global variable and have a value of 5");' ```
## Challenge Seed
```js // Declare your variable here function fun1() { // Assign 5 to oopsGlobal Here } // Only change code above this line function fun2() { var output = ""; if (typeof myGlobal != "undefined") { output += "myGlobal: " + myGlobal; } if (typeof oopsGlobal != "undefined") { output += " oopsGlobal: " + oopsGlobal; } console.log(output); } ```
### Before Test
```js var logOutput = ""; var originalConsole = console function capture() { var nativeLog = console.log; console.log = function (message) { logOutput = message; if(nativeLog.apply) { nativeLog.apply(originalConsole, arguments); } else { var nativeMsg = Array.prototype.slice.apply(arguments).join(' '); nativeLog(nativeMsg); } }; } function uncapture() { console.log = originalConsole.log; } var oopsGlobal; capture(); ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```