关于javascript:为什么用`var`定义的匿名函数可以在全局范围内调用?

Why can anonymous functions defined with `var` be called in global scope?

1
2
3
4
5
6
7
8
9
10
11
12
function setupSomeGlobals() {
  // Local variable that ends up within closure
  var num = 666;
  // Store some references to functions as global variables
  var gAlertNumber = function() { console.log(num); }
}

setupSomeGlobals();

gAlertNumber(); //works, WHY?!!

console.log(num); //does not work, GOOD

我希望galertNumber()不能在setupSomeGlobals()函数之外工作…


(P)变量与EDOCX1的英文字母0保持一致,是地方性的,不能从外部进入。(p)(P)如果你在安慰中运行这一点,它更像是你可能通过早期的三种方式污染了全球名称空间。Open a new tab and run the code again.(p)