Files
freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions.md
2022-12-19 20:41:09 +02:00

2.3 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244bf النطاق المحلي والوظائف (Local Scope and Functions) 1 https://scrimba.com/c/cd62NhM 18227 local-scope-and-functions

--description--

المتغيرات التي المعلنا داخل وظيفة، وكذلك الوسائط (parameters) للوظيفة لديها نطاق و يكون محلي. وهذا يعني أنها لا تبد مرئية إلا في إطار الوظيفة.

هذه وظيفة myTest مع متغير محلي يسمى loc.

function myTest() {
  const loc = "foo";
  console.log(loc);
}

myTest();
console.log(loc);

استدعاء الوظيفة myTest() ستعرض المقطع foo في وحدة التحكم. سيؤدي سطر console.log(loc) (خارج وظيفة myTest) إلى ظهور خطأ، حيث إن loc لم يتم تعريفها خارج الوظيفة.

--instructions--

يحتوي المحرر على اثنين console.log لمساعدتك على رؤية ما يحدث. تحقق من وحدة التحكم خلال كتابة الكود لترى كيف يتغير. اعلن متغير محلي اسمه myVar داخل myLocalScope وفعّل الاختبارات.

ملاحظة: وحدة التحكم ستظل تعرض ReferenceError: myVar is not defined، ولكن هذا لن يتسبب في فشل الاختبارات.

--hints--

لا ينبغي أن يحتوي الكود على متغير عام يدعي myVar.

function declared() {
  myVar;
}

assert.throws(declared, ReferenceError);

يجب عليك إضافة متغير محلي يدعي myVar.

assert(
  /functionmyLocalScope\(\)\{.*(var|let|const)myVar[\s\S]*}/.test(
    __helpers.removeWhiteSpace(code)
  )
);

--seed--

--seed-contents--

function myLocalScope() {
  // Only change code below this line

  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);

--solutions--

function myLocalScope() {
  // Only change code below this line
  let myVar;
  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);