如果我理解正确,在函数内部不使用关键字 var 声明变量将创建一个全局范围的变量。
但是当从其容器函数外部访问变量时,我得到这个“ReferenceError: oopsGlobal is not defined”。
,,,
// Declare the myGlobal variable below this line
var myGlobal = 10
function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal = 5
}
// 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);
}
console.log(oopsGlobal) // ReferenceError: oopsGlobal is not defined
,,,
慕码人8056858
人到中年有点甜
相关分类