Skip to content

Instantly share code, notes, and snippets.

@hdvianna
Created April 2, 2019 19:47
Show Gist options
  • Save hdvianna/3f99097b84a6337177137b82e579b944 to your computer and use it in GitHub Desktop.
Save hdvianna/3f99097b84a6337177137b82e579b944 to your computer and use it in GitHub Desktop.
JS Closure Examples
/**
* Bind com let i = 0
* Bind com let i = 1
* Bind com let i = 2
* Bind com let i = 3
* Bind com let i = 4
*/
for(let i = 0; i < 5; i++) {
setTimeout(function(){
console.log('Bind com let i = '.concat(i));
}, 1000);
}
/**
* Bind com var k = 5
* Bind com var k = 5
* Bind com var k = 5
* Bind com var k = 5
* Bind com var k = 5
*/
for(var k = 0; k < 5; k++) {
setTimeout(function(){
console.log('Bind com var k = '.concat(k));
}, 1000);
}
/**
* Bind com let j = 0
* Bind com let j = 1
* Bind com let j = 2
* Bind com let j = 3
* Bind com let j = 4
*/
for(var j = 0; j < 5; j++) {
(function clo(j) {
setTimeout(function(){
console.log('Bind com funcao anonima j = '.concat(j));
}, 1000);
})(j);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment