Advertisement
samimwebdev

7.Scope closure hoisting destructuring rest spread

Feb 27th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // const person = {
  2. //   firstName: 'samim',
  3. //   lastName: 'fazlu',
  4. //   age: 30,
  5. //   fullName() {
  6. //     return this.firstName + '-' + this.lastName + '-' + this.age
  7. //   },
  8. // }
  9.  
  10. //pulling out of property from object or array
  11. // const { firstName, fullName, ...restValues } = person
  12. // console.log(fullName)
  13. // // console.log(fullName.call(person))
  14. // // console.log(myAge)
  15. // console.log(restValues)
  16.  
  17. // const arr = ['samim', 'Fazlu', 30]
  18. // // const [fName, , age] = arr
  19. // const [fName,... restArr] = arr
  20. // console.log(arr[0])
  21. // console.log(restArr)
  22. // console.log(fName, age)
  23.  
  24. // console.log(person.firstName)
  25. // console.log(person.lastName)
  26. // console.log(person.age)
  27. // console.log(person.fullName)
  28.  
  29. //rest (...) Destructuring
  30. //spread(...)
  31.  
  32. // const personWithProfession = {
  33. //   ...person,
  34. //   profession: 'App Developer',
  35. // }
  36.  
  37. // console.log(personWithProfession)
  38.  
  39. // const newWdArr = [...arr, 'web developer']
  40. // console.log(newWdArr)
  41.  
  42. // function printPerson(fName, lName, age) {
  43. // }
  44.  
  45. // const person = {
  46. //   firstName: 'samim',
  47. //   lastName: 'fazlu',
  48. //   age: 30,
  49. // }
  50.  
  51. // printPerson('samim', "hasan")
  52.  
  53. //scope (the world where data is defined and can be accessed)
  54. //var (functional scope, global scope)
  55. //let, const(block scope {}, global scope)
  56. //if variable is declared globally with var or let or const keyword it can be accessed any part of the script
  57.  
  58. // var a = 10
  59.  
  60. // function b() {
  61. //   var c = 20
  62. // //   console.log(a)
  63. //   console.log(c)
  64. //   return c
  65. // }
  66. // b()
  67.  
  68. // b()
  69. // console.log(a)
  70. // // console.log(c)
  71.  
  72. //if let ,const  is declared inside block{} then it will be available only in inside the block{}
  73. //otherwise it will be considered as global(global variable can be accessed from anywhere)
  74. //loop gotcha
  75.  
  76. // const d = 40
  77. // {
  78. //   var e = 50
  79. //   const blockVar = 30
  80. //   console.log(blockVar)
  81. // // console.log(d)
  82. // }
  83.  
  84. // console.log(e)
  85. // console.log(blockVar)
  86.  
  87. // for (let i = 0; i < 10; i++) {
  88. //   console.log(i)
  89. // }
  90.  
  91. // console.log(i)
  92. //scope chain
  93. // From children scope you can access parent scoped variable (children can access parent property) -scope chain
  94. // const a = 20
  95. // function showScope() {
  96. //    const a = 10
  97. //   {
  98. //     const a = 100
  99. //     console.log(a)
  100. //     const b = 20
  101. //     console.log(b)
  102. //   }
  103. //   console.log(a)
  104. //   console.log(b)
  105. // }
  106.  
  107. // showScope()
  108.  
  109. //closure scope
  110. //variable scope is limited to it's function lifespan
  111. //After function execution is over(function running) it's scoped variable is vanished except the variable reference from inner function
  112.  
  113. // function sum(num1) {
  114. //   num1 = 3
  115. //   return (num2) => {
  116. //     return num1 + num2
  117. //   }
  118. // }
  119.  
  120. // const innerFunc = sum(3)
  121. // console.log(innerFunc(4))
  122.  
  123. //Hoisting (lifting up variable and accessing in different part)
  124. // console.log(a)
  125. // console.log(func())
  126. // console.log(hello())
  127.  
  128. // const a = 10
  129. // const func = function () {
  130. //   console.log('Hi')
  131. // }
  132.  
  133. //   console.log(func())
  134. // function hello() {
  135. //   return 'hello'
  136. // }
  137.  
  138. //preparation phase
  139. //function declaration (statement) in memory
  140. //variable declaration (memory)
  141. //variable value is not set
  142. // var a;
  143. //var func
  144. //function hello(){
  145. //     return 'hello'
  146. // }
  147.  
  148. //execution phase
  149. // variable value assign
  150. //calling..
  151. // console.log(a) //undefined
  152. // console.log(func())
  153. // console.log(hello()) //'hello'
  154. //a = 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement