Last active
March 11, 2018 17:42
Revisions
-
awilson28 revised this gist
Mar 13, 2015 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,11 +8,10 @@ for (var i = 0; i <= 3; i++) { /* fixing this with an Immediately Invoked Function Expression (IIFE) The IIFE creates a new scope for each iteration, providing the callback in setTimeout with a new variable i that is mapped to the right per-iteration value */ for (var j = 0; j <= 3; j++) { (function(k){ @@ -24,7 +23,7 @@ for (var j = 0; j <= 3; j++) { /* above, we define an IIFE that takes a parameter k and then console.log's that k and then we invoke that IIFE with j: the IIFE closes over j so that the callback function has a copy of j at 0, 1, 2, and 3. Note that we can also just keep j everywhere as shown in the code directly below: */ -
awilson28 revised this gist
Mar 13, 2015 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,9 +10,9 @@ for (var i = 0; i <= 3; i++) { /* fixing this with an Immediately Invoked Function Expression (IFFE) The use of an IIFE inside each iteration creates a new scope for each iteration, which yields timeout function callbacks a new scope for each iteration: each scope has a variable i with the right per-iteration value in it for us to access */ for (var j = 0; j <= 3; j++) { (function(k){ @@ -47,4 +47,6 @@ for (var i = 0; i <= 3; i++) { }, 1000) })(); } ``` -
awilson28 revised this gist
Mar 2, 2015 . 1 changed file with 8 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,12 @@ for (var i = 0; i <= 3; i++) { /* fixing this with an Immediately Invoked Function Expression (IFFE) The use of an IIFE inside each iteration creates a new scope for each iteration, which gives our timeout function callbacks the opportunity to close over a new scope for each iteration, one which had a variable with the right per-iteration value in it for us to access */ for (var j = 0; j <= 3; j++) { (function(k){ setTimeout(function(){ @@ -17,6 +22,8 @@ for (var j = 0; j <= 3; j++) { })(j) } /* so we define an IIFE that takes a variable k and then console.log's that k and then we invoke that IIFE with j: the IIFE closes over j so that the callback function has a copy of j at 0, 1, 2, and 3. Note that we can also @@ -32,10 +39,6 @@ for (var j = 0; j <= 3; j++){ //We can achieve the same functionality like so: for (var i = 0; i <= 3; i++) { (function() { var j = i; -
awilson28 revised this gist
Mar 2, 2015 . 1 changed file with 11 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,8 +18,17 @@ for (var j = 0; j <= 3; j++) { } /* so we define an IIFE that takes a variable k and then console.log's that k and then we invoke that IIFE with j: the IIFE closes over j so that the callback function has a copy of j at 0, 1, 2, and 3. Note that we can also just keep j everywhere as shown in the code directly below: */ for (var j = 0; j <= 3; j++){ (function(j) { setTimeout(function() { console.log(j); }, 1000) })(j) } //We can achieve the same functionality like so: -
awilson28 revised this gist
Mar 2, 2015 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -23,6 +23,10 @@ callback function has a copy of j at 0, 1, 2, and 3 */ //We can achieve the same functionality like so: /* The use of an IIFE inside each iteration creates a new scope for each iteration, which gives our timeout function callbacks the opportunity to close over a new scope for each iteration, one which had a variable with the right per-iteration value in it for us to access */ for (var i = 0; i <= 3; i++) { (function() { var j = i; -
awilson28 revised this gist
Mar 2, 2015 . 1 changed file with 19 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,12 +10,25 @@ for (var i = 0; i <= 3; i++) { //fixing this with an Immediately Invoked Function Expression (IFFE) for (var j = 0; j <= 3; j++) { (function(k){ setTimeout(function(){ console.log(k); }, 0) })(j) } /* so we define an IIFE that takes a variable k and then console.log's that k and then we invoke that IIFE with j the IIFE closes over j so that the callback function has a copy of j at 0, 1, 2, and 3 */ //We can achieve the same functionality like so: for (var i = 0; i <= 3; i++) { (function() { var j = i; setTimeout(function() { console.log(j) }, 1000) })(); } ``` -
awilson28 revised this gist
Jan 22, 2015 . 1 changed file with 9 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,10 +8,14 @@ for (var i = 0; i <= 3; i++) { //fixing this with an Immediately Invoked Function Expression (IFFE) for (var j = 0; j <= 3; j++) { (function(k){setTimeout(function(){ console.log(k); }, 0)})(j) } //so we define an IIFE that takes a variable k and then console.log's that k //and then we invoke that IIFE with j //the IIFE closes over j so that the callback function has a copy of j at 0, 1, 2, and 3 ``` -
awilson28 revised this gist
Jan 22, 2015 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,4 +5,13 @@ for (var i = 0; i <= 3; i++) { console.log(i); }, 0); } //fixing this with an IFFE for (var i = 0; i <= 3; i++) { (function(i){setTimeout(function(){ console.log(i); }, 0)})(i) } ``` -
awilson28 created this gist
Jan 21, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ ``` javascript //run this in your console for (var i = 0; i <= 3; i++) { setTimeout(function(){ console.log(i); }, 0); } ```