JavaScript ES5 and ES6 setTimeout
Blogs20172017-04-24
JavaScript: ES5, ES6 setTimeout
Are the 2 following closures same?
// closure
for(var i=1; i<6; i++) {
setTimeout((function(i) {
return function() {
console.log(i);
}
}(i)), 1000)
}
for(var i=1; i<6; i++) {
setTimeout( ((n) => () => console.log(n))(i), 1000)
}Are the following 2 IIFE same?
for(var i=1; i<6; i++) {
(function(i) {
setTimeout(function() {
console.log(i)
}, 1000)
})(i)
}
for(var i=1; i<6; i++) {
((i) => setTimeout(() => console.log(i), 1000))(i)
}the first use closure, the second IIFE.
