-1
JavaScript中的函數範圍如何工作?函數作用域如何在JavaScript中工作?
JavaScript中的函數範圍如何工作?函數作用域如何在JavaScript中工作?
// Create a new function = new scope
(function() {
var a = 1; // create a new variable in this scope
if (true) { // create a new block
var a = 2; // create "new" variable with same name,
// thinking it is "local" to this block
// (which it isn't, because it's not a block scope)
}
alert(a); // yields 2 (with a block scope, a would still be 1)
})();
alert(typeof a); // yields "undefined", because a is local to the function scope above
真棒例子,正是我需要得到。 +1。 並感謝這個有效的工具 - jsfiddle。 – himanshupareek66