Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // note : every scope can access inner scope and outer scope
- function outer() {
- let outer_value = “outer scope”
- function inner() {
- let inner_value = “inner scope”
- console.log(outer_value, inner_value)
- }
- inner();
- }
- outer()
- // we can only call inner function in outer function scope
- //but that not powerful , so if we want to use inner we should return it
- function outerfun() {
- let outer_value = “outer scope”
- return function inner() {
- let inner_value = “inner scope”
- console.log(outer_value, inner_value)
- }
- }
- let innerfun=outerfun()
- innerfun()
- // that‘s called Closure
- // The ability to access inner function by return it as value
- // function return function
- // to use inner function you should execute it
- function setLocal(n) {
- let local = n;
- return () => console.log(local);
- }
- let local1 = setLocal(6);
- local1() //6
- let local2 = setLocal(9);
- local2() //9
- //In the example, multiplier is called , creates an scope and stored the value of factor
- // and return The function
- //when twice is called it multiplies its argument by 2 (factor).
- function multiplier(factor) {
- return number => number * factor;
- }
- let twice = multiplier(2);
- console.log(twice(5)) // 10
- 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement