Skip to content

Instantly share code, notes, and snippets.

@rayinla
Last active March 31, 2019 09:06
Show Gist options
  • Save rayinla/85c93af47069395f0f94cbc73bfdb692 to your computer and use it in GitHub Desktop.
Save rayinla/85c93af47069395f0f94cbc73bfdb692 to your computer and use it in GitHub Desktop.
/*
Closures cannot access the arguments object of the parent,
but, because functions are first class objects, we can pass a function as a parameter.
The closure can now access the arguments object of the function that is passesd as a parameter.
So, there is no confusion as to which arguments object we want the closure to access.
We're basically taking advantage of its limitations
*/
function demoMemo(func){
//we must return a function in order to keep state
//this will be more apparant in a recursive example
return function () {
console.log(func); // > function (num){num + num}
console.log(arguments[0]); // > 1
}
}
// Our function expression here calls demoMemo and passes it an anonymous function.
var adder = demoMemo(function(num){
num + num;
})
//Once that is passed, as you can see, when we call our function expression,
//we have access to all the properties of the function we want to memoize
adder(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment