2020-10-06 23:10:08 +05:30

1.6 KiB

id, challengeType, videoUrl, forumTopicId, localeTitle
id challengeType videoUrl forumTopicId localeTitle
56533eb9ac21ba0edf2244c0 1 https://scrimba.com/c/c2QwKH2 18194 函数中的全局作用域和局部作用域

Description

一个程序中有可能具有相同名称的局部变量 和全局变量。在这种情况下,局部变量将会优先于全局变量。 下面为例:
var someVar = "Hat";
function myFun() {
  var someVar = "Head";
  return someVar;
}

函数myFun将会返回"Head",因为局部变量优先级更高。

Instructions

myOutfit添加一个局部变量来覆盖outerWear的值为"sweater"

Tests

tests:
  - text: 不要修改全局变量<code>outerWear</code>的值。
    testString: assert(outerWear === "T-Shirt");
  - text: <code>myOutfit</code>应该返回<code>"sweater"</code>。
    testString: assert(myOutfit() === "sweater");
  - text: 不要修改<code>return</code>语句。
    testString: assert(/return outerWear/.test(code));

Challenge Seed

// Setup
var outerWear = "T-Shirt";

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



  // Only change code above this line
  return outerWear;
}

myOutfit();

Solution

var outerWear = "T-Shirt";
function myOutfit() {
  var outerWear = "sweater";
  return outerWear;
}