Last active
December 19, 2015 10:59
-
-
Save themindfuldev/5944520 to your computer and use it in GitHub Desktop.
This gist just shows some good usage and bad usage examples about Hoisting concept in Javascript.
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 characters
function stepSum() { | |
var total = 0; | |
for (var i = 0; i < arguments.length; i++) { | |
var parameter = arguments[i]; | |
if (typeof(parameter) !== 'number') { | |
parameter = parseInt(parameter); | |
} | |
setTimeout(function() { | |
if (!isNaN(parameter)) { | |
total += parameter; | |
console.log(i + ") adding " + parameter + | |
", total is now " + total); | |
} | |
}, i*1000); | |
} | |
return "the stepSum has been triggered..."; | |
} |
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 characters
stepSum(3, 2, 1); |
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 characters
function stepSum() { | |
var parameter, printStep, | |
total = 0, | |
i = 0; | |
// printStep function | |
printStep = function(parameter, step) { | |
step++; | |
setTimeout(function() { | |
if (!isNaN(parameter)) { | |
total += parameter; | |
console.log(step + ") adding " + parameter + | |
", total is now " + total); | |
} | |
}, step * 1000); | |
}; | |
// Iterating | |
for (i = 0; i < arguments.length; i++) { | |
parameter = arguments[i]; | |
if (typeof(parameter) !== 'number') { | |
parameter = parseInt(parameter); | |
} | |
printStep(parameter, i); | |
} | |
return "the stepSum has been triggered..."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment