Advertisement
samimwebdev

scope, closure, execution context(class-7)

Dec 15th, 2021
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Execution context
  2. //javascript scope
  3. //javascript closure
  4. //javascript hoisting
  5. //Visualization (all)
  6.  
  7. //Execution context
  8.  
  9.   //global (outside of function)
  10.  
  11.   //functional (inside of a function)
  12.  
  13.  
  14.  
  15.  
  16.  
  17. /////////// global
  18.  
  19. //   const num1 = 10
  20. //   const num2 = 20
  21. // functional execution context
  22. //   function add(){
  23. //     return num1 + num2
  24. //   }
  25.  
  26. //   console.log(add())
  27.  
  28. //  /// functional execution context
  29. //   function subtract(){
  30. //     return num1 - num2
  31. //   }
  32. //   console.log(subtract())
  33.  
  34.  
  35. //   /////
  36.  
  37.  
  38.  
  39. //scope
  40.   //global scope (outside of function)
  41.  
  42.   //functional scope (var)
  43.   //block scope(ES6) enclosed by  {}
  44.  
  45.  
  46. //global
  47. // const a = 10
  48.  
  49.  
  50. //block scope ( applicable for let, const)
  51. //   if(true){
  52. //     var d = 20
  53. //     const c = 30
  54. //   }
  55.  
  56. //   console.log(d)
  57.  
  58. // //   console.log(c)
  59.  
  60. //  function sum (){
  61. //      //local
  62. //      const b = 10
  63. //      var e = 20
  64.  
  65. //     //  console.log(f)
  66. //      function inner(){
  67. //         const a  = 100
  68. //        console.log(a)
  69. //        const f = 20
  70. //      }
  71. //      inner()
  72. //     console.log(a)
  73. //  }
  74.  
  75.  
  76.  //not accessible (local scope)
  77. // console.log(e)
  78.  
  79. //  sum()
  80.  
  81.  
  82.  
  83. //closure scope
  84.  
  85. // function greet(greetings){
  86. //     //greetings = 'hello'
  87. //  return name =>{
  88. //      //name  = 'samim'
  89. //     return greetings + ' ' + name
  90. //  }
  91. // }
  92.  
  93.  
  94. // const secondFn = greet('hello')
  95.  
  96. // console.log(secondFn('samim'))
  97. // console.log(secondFn('samim'))
  98.  
  99.  
  100.  
  101.  
  102. //javascript hoisting
  103.  
  104. // console.log(a) //temporal dead zone
  105. // console.log(x)
  106. // console.log(sum(10, 20))
  107. // console.log(subtract(20, 10))
  108.  
  109. // const  a = 10
  110. // var x = 20
  111.  
  112.  
  113. // function sum(num1, num2){
  114. //     return  num1 + num2
  115. // }
  116. // console.log(sum(10, 20))
  117.  
  118. // var subtract = function (num1, num2){
  119. //     return  num1 - num2
  120. // }
  121.  
  122. // console.log(subtract(20, 10))
  123.  
  124. // console.log(a)
  125.  
  126.  
  127.  
  128. //Hoisting
  129. //creation stage
  130. //variable declaration
  131. //take function statement in memory
  132.  
  133.  
  134. //execution stage
  135. //variable value assignment
  136. //calling (run)
  137.  
  138.  
  139. //creation stage
  140. // var x;
  141. // var a;
  142. // function sum(num1, num2){
  143. //    return  num1 + num2
  144. // }
  145.  
  146. // var subtract;
  147.  
  148.  
  149. // //execution stage
  150. // console.log(a)
  151. // console.log(x)
  152. // console.log(sum(10, 20))
  153. // console.log(subtract(20, 10))
  154.  
  155. // var a = 10
  156. // var x = 20
  157.  
  158. // a = 10
  159. // x = 20
  160. // subtract = function (num1, num2){
  161. //    return  num1 - num2
  162. // }
  163. // console.log(subtract(20, 10))
  164.  
  165. // console.log(a)
  166.  
  167.  
  168.  
  169. //var let const
  170.  
  171. //var- attached with window, re-declaration, Hoisting, functional scope
  172. //let - Not attached with window, No hoisting(?), no re-declaration but can be re-assigned can be  block scope
  173. //const - same as let,  can't be re-assigned
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement