3.2 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
| id | title | challengeType | videoUrl | forumTopicId | dashedName |
|---|---|---|---|---|---|
| 56533eb9ac21ba0edf2244be | النطاق الشامل والوظائف | 1 | https://scrimba.com/c/cQM7mCN | 18193 | global-scope-and-functions |
--description--
في JavaScript، يشير النطاق إلى رؤية المتغيرات. يكون إلى المتغيرات التي تم تعريفها خارج الوظيفة نطاق يسمي شامل. وهذا يعني أنه يمكن رؤيتها في كل مكان في التعليمات البرمجية JavaScript الخاص بك.
يتم تعريف المتغيرات دون استخدام الكلمات الآتية let أو const يتم إنشاؤها تلقائيًا في نطاق global. هذا يمكن أن يؤدي إلى عواقب غير مقصودة في مكان آخر من التعليمات البرمجية الخاص بك أو عند تشغيل الوظيفة مرة أخرى. يجب عليك دائماً تعريف المتغيرات الخاصة بك باستخدام let أو const.
--instructions--
باستخدام let أو const، عرف متغير شامل يسمى myGlobal خارج الوظيفة ما. قم بتهيئته بقيمة 10.
داخل الوظيفة fun1، عيّّن 5 إلى oopsGlobal ولكن دون استخدام var, أو let, أو const.
--hints--
يجب أن يتم تعريف myGlobal
assert(typeof myGlobal != 'undefined');
يجب أن يساوي myGlobal قيمة 10
assert(myGlobal === 10);
يجب تعريف myGlobal باستخدام let أو const
assert(/(let|const)\s+myGlobal/.test(code));
يجب أن يكون متغير oopsGlobal شامل وأن يساوي 5
assert(typeof oopsGlobal != 'undefined' && oopsGlobal === 5);
--seed--
--before-user-code--
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-user-code--
fun1();
fun2();
uncapture();
(function() { return logOutput || "console.log never called"; })();
--seed-contents--
// Declare the myGlobal variable below this line
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);
}
--solutions--
const myGlobal = 10;
function fun1() {
oopsGlobal = 5;
}
function fun2() {
var output = "";
if(typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if(typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}