Guest User

Untitled

a guest
Jan 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. /*
  2. Closures cannot access the arguments object of the parent,
  3. but, because functions are first class objects, we can pass a function as a parameter.
  4. The closure can now access the arguments object of the function that is passesd as a parameter.
  5. So, there is no confusion as to which arguments object we want the closure to access.
  6. We're basically taking advantage of its limitations
  7. */
  8. function demoMemo(func){
  9. //we must return a function in order to keep state
  10. //this will be more apparant in a recursive example
  11.  
  12. return function () {
  13. console.log(func); // > function (num){num + num}
  14. console.log(arguments[0]); // > 1
  15. }
  16. }
  17.  
  18. // Our function expression here calls demoMemo and passes it an anonymous function.
  19. var adder = demoMemo(function(num){
  20. num + num;
  21. })
  22.  
  23. //Once that is passed, as you can see, when we call our function expression,
  24. //we have access to all the properties of the function we want to memoize
  25.  
  26. adder(1);
Add Comment
Please, Sign In to add comment