Created
September 23, 2016 20:28
-
-
Save domfarolino/a7fd9bc91e97b7b39b4027865544d237 to your computer and use it in GitHub Desktop.
2nd example explaining closures to Pat
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
var outerFunction = function(outerFunctionState) { | |
var innerFunction = function() { | |
console.log("Time difference: " + (Date.now() - outerFunctionState)/1000); | |
} | |
return innerFunction; | |
} | |
var generatedFunction1 = outerFunction(Date.now()); | |
// Next to no time at all from when we generateFunction1 and call it | |
generatedFunction1(); | |
// Artificial time delay | |
for (var i = 0; i < 1000000000; ++i) {} | |
for (var i = 0; i < 1000000000; ++i) {} | |
// But when we call it later we get the difference of the current time | |
// and the outerFunctionState which is the time when we first generated | |
// the function | |
generatedFunction1(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment