Skip to content

Instantly share code, notes, and snippets.

@domfarolino
Created September 23, 2016 20:28
Show Gist options
  • Save domfarolino/a7fd9bc91e97b7b39b4027865544d237 to your computer and use it in GitHub Desktop.
Save domfarolino/a7fd9bc91e97b7b39b4027865544d237 to your computer and use it in GitHub Desktop.
2nd example explaining closures to Pat
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