Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. // note : every scope can access inner scope and outer scope
  2. function outer() {
  3. let outer_value = “outer scope”
  4. function inner() {
  5. let inner_value = “inner scope”
  6. console.log(outer_value, inner_value)
  7. }
  8. inner();
  9. }
  10. outer()
  11. // we can only call inner function in outer function scope
  12. //but that not powerful , so if we want to use inner we should return it
  13. function outerfun() {
  14. let outer_value = “outer scope”
  15. return function inner() {
  16. let inner_value = “inner scope”
  17. console.log(outer_value, inner_value)
  18. }
  19. }
  20. let innerfun=outerfun()
  21. innerfun()
  22.  
  23. // that‘s called Closure
  24. // The ability to access inner function by return it as value
  25. // function return function
  26. // to use inner function you should execute it
  27. function setLocal(n) {
  28. let local = n;
  29. return () => console.log(local);
  30. }
  31. let local1 = setLocal(6);
  32. local1() //6
  33. let local2 = setLocal(9);
  34. local2() //9
  35.  
  36. //In the example, multiplier is called , creates an scope and stored the value of factor
  37. // and return The function
  38. //when twice is called it multiplies its argument by 2 (factor).
  39. function multiplier(factor) {
  40. return number => number * factor;
  41. }
  42. let twice = multiplier(2);
  43. console.log(twice(5)) // 10
  44. 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement