Skip to content

Instantly share code, notes, and snippets.

@SabrinaMarkon
Forked from rayinla/memo-strip.js
Created March 31, 2019 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SabrinaMarkon/c78e504b33eb199c6e686547e2a1cf44 to your computer and use it in GitHub Desktop.
Save SabrinaMarkon/c78e504b33eb199c6e686547e2a1cf44 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