[英]Why is the value not assigned to the variable in case of an IIFE?
(function() {
var a = b = 3;
})();
console.log(a, b);
(function() {
var x = y = 3;
console.log(x, y);
})();
這些是非常簡單的程序。 但是我很困惑,為什么在第一個示例程序的輸出中b
在b
的值為3而在第二個示例程序的x
和y
都為3 a
情況下卻未定義b
?
(function() {
var a = b = 3;
})();
console.log(a, b);
創建IIFE時會創建並調用一個函數。這里是局部變量,並且變量具有局部范圍,因此您不能在外部訪問它
您沒有在b前面使用var,因此它成為全局變量,因此也反映在函數范圍之外
基本上var a = b = 3;
等價於var a = (b = 3);
並且您可能知道在JavaScript中可以在不使用var
關鍵字的情況下定義變量,並且當您聲明不使用var
關鍵字的變量時,該變量將位於全局范圍內,因此在IIFE之外,您將b記錄為3,但是當您嘗試記錄a
變量a
未定義,因為使用var
關鍵字的變量聲明具有本地范圍,並且無法在其范圍之外訪問。
因此,這告訴您:
var a = b = 3;
//b = 3; //global scope //first it assigns b = 3.
//var a = 3;//local scope //then assigns a = 3.
因為在第一個函數中,您聲明了局部變量var a
並在函數外部打印,該變量無法訪問局部變量,因此未定義。
和第二個函數,您可以在訪問局部變量的函數內部打印,因此效果很好
給出的所有答案都是正確的。 我只是想展示使用您自己的代碼正在發生的事情,並使涉及此問題的所有內容對於遇到此問題的其他人都非常清楚。 而且我這樣做很有趣; D
這里發生了三件事:
關聯性:當像給定的代碼那樣進行分配時,所有內容都從右向左流動,而不是從左向右流動。 因此表達式將有效地重新排序。
范圍變量:如果您不對每個變量都使用var,則給定變量將被視為全局變量,從而泄漏到局部函數范圍之外。 可以同時使用逗號設置多個變量( var a = 1, b = 2;
)。 這有效地將var
復制到每個新變量,從而使每個變量在本地范圍內(相當於: var a = 1; var b = 2;
)。
全局范圍與局部范圍:同一范圍內的代碼(在本例中為同一函數局部)可以看到該范圍內的變量。 該范圍之外的代碼(在這種情況下,在函數之外)將只能看到未綁定在該函數內的全局變量。
讓我們看一下原始代碼,以清除問題:
(function() {
// var a = b = 3;
b = 3; // no var, gets outside of the function's scope (known as global scope)
var a = b; // with var, stays inside the function's scope (known as local scope)
})();
// we're outside of the function now
// we can't see any of the variables local to the function's scope, but we *can* see variables in the global scope
console.log(a, b);
// there is no `a` variable set in the global scope, so that value is given as `undefined`
// there is a `b` variable set in the global scope, so its value (3) is given
(function() {
// var x = y = 3;
y = 3; // global
var x = y; // local
// we're still inside the function, so we can still see its scoped variables
console.log(x, y);
// `x` is local, so its value (3) is used
// `y` is global, and global variables can be seen from anywhere. so its value (3) is also used
})();
您可以通過將代碼更改為以下內容來使所有內容都本地化:
(function() {
var a = b = 3; // local a, global b
// the original code
var b, a = b = 3; // local a, local b
// because of the comma, the `var` applies to all variables;
equivalent to:
// var b; // local; further assignments to `b` will stay local
// b = 3; // local
// var a = b; // local
var b = 3, a = b; // local a, local b; equivalent to:
// var b = 3;
// var a = b;
var a, b; a = b = 3; // local a, local b; equivalent to:
// var a; var b;
// b = 3; // local
// a = b; // local
})();
或者通過完全不使用var
來使其全部變為全局,如下所示:
(function() {
var a = b = 3; // local a, global b
// the original code
a = b = 3; // global a, global b; equivalent to:
// b = 3; // global
// a = b; // global
})();
如您所見,讓變量泄漏到函數范圍之外很容易。 因此,在編碼時,我們需要確保將var
應用於創建的所有變量,但極少數情況下,我們實際上希望全局設置變量。
有一點要記住建立一個本地時var
是, 該變量將“影子”全球任何具有相同名稱的變量。 運行代碼時,將使用任何局部作用域的變量, 直到它在鏈的更高位置尋找相同的變量名(如果該函數嵌套在另一個函數中,或者最終嵌套到全局范圍)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.