โscopes matter with closures
JS โฉ value โฉ function โฉ closure โฉ scopes matter
the scoping effect of the initialization block can be understood as if the declaration happens within the loop body, but just happens to be accessible within the condition
and update
parts.
๐ MDN
replit๏ผscopes matter with closures
const { log } = console;
// โญโโ i โโโฎ <--- โญ `i` is "block-scoped"
for (let i = 0; i < 3; i++) {
// โญ every time "block scope" is entered,
// a new/different `i` is captured by closure.
//
// โญโ closure โโโฎ
setTimeout( () => log(i) , 1000); // 0, 1, 2 โญ
}
let j = 0; // โ `j` is in "outer scope"
for ( ; j < 3; j++) {
// โ every closure is capturing the same `j` variable
//
// โญโ closure โโโฎ
setTimeout( () => log(j) , 2000); // 3, 3, 3 โ (j = 3 after for-loop)
}
var keep1 = [];
// โญโโ i โโโฎ <--- โ "var" has no block scope
for (var i = 0; i < 3; i++) {
// โ every closure is keeping the "same" `i`.
keep1[i] = function keepI(){
return i;
};
}
var keep2 = [];
// โญโโ i โโโฎ <--- โญ "let" is block-scoped
for (let i = 0; i < 3; i++) {
// โญ `i` is block-scoped, every time block scope is entered,
// a new/different `i` is created & kept by closure.
keep2[i] = function keepI(){
return i;
};
}
[
keep1.map(f => f()), // โ [ 3, 3, 3 ]
keep2.map(f => f()), // โญ [ 0, 1, 2 ]
].forEach(x => log(x));
for loop
ๅฆๆๅฆ "for-init" scope ไธญๆ่ชช๏ผ let ่ฎๆธๆดปๅจ้ๅ็นๆฎ็็ฏๅไธญ๏ผ้ฃ้บผๆฌไพ็ "i" ๆ่ฉฒๅฐฑๆดปๅจ้ๅ็ฏๅๅ ง๏ผๅฐๅ ง้จ็ "block scope" ไพ่ชช๏ผๆ่ฉฒไน็ฎๆฏไธๅ "outer scope"๏ผ็ ง็่ชช่กจ็พ็ๆนๅผๆ่ฉฒ่ฆ่ท "j" ไธๆจฃๆๆฏ๏ผไฝ้่ฃก็ "i" ่กจ็พๅพๅปๅๆฏๅจ "block scope" ่ฃ้ขๆ็ๅบไพ็ใๆฐ่ฎๆธใไธๆจฃ๏ผๅฏฆๅจๆ้ปๆชโ๏ธ
ๆไปฅ็ไพ๏ผๅจๅฏฆ้ไฝฟ็จไธ๏ผ"i" ็่ก็บๆดๅๆฏ "block-scoped" ไธไบ โ
Last updated
Was this helpful?