728x90
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// closure.js | |
// var | |
var funcs = []; | |
for(var i = 0; i < 10; i += 1) { | |
funcs.push( function(){return i;} ); | |
} | |
console.log('i : ' + i); // 10 | |
funcs.forEach(function(func){ | |
func(); // 10 * ten times | |
}); | |
// let | |
let funcs2 = []; | |
for(let j = 0; j < 10; j += 1) { | |
funcs2.push( function(){return j;} ); | |
} | |
funcs2.forEach(function(func2){ | |
func2(); // 0, 1, 2, 3, 4, ..., 9 | |
}); |
728x90
반응형
'Javascript' 카테고리의 다른 글
TDZ (0) | 2018.11.22 |
---|---|
non TDZ (0) | 2018.11.22 |
fibonacci.js (0) | 2018.11.08 |
iteration from 'Learning Javascript' (0) | 2018.11.08 |
화살표 함수 Arrow function of javascript (0) | 2018.09.28 |