Advertisement
samimwebdev

class-07(code sample)

Oct 9th, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //javascript "this"
  2.  
  3. //inside of the function (functional context)
  4. //outside of function (global context)
  5.  
  6. //window obj(global context) -Don't use
  7. console.log(this)
  8. ;('use strict')
  9. function whatIsThis() {
  10.   //In strict mode  this indicates undefined otherwise widow object
  11.   console.log(this)
  12. }
  13.  
  14. whatIsThis() //plain function call (window)
  15.  
  16. const calculator = {
  17.   num1: 2,
  18.   num2: 3,
  19.   add() {
  20.     console.log(this.num1 + this.num2)
  21.     // console.log(this)
  22.     const $this = this
  23.     function multiply() {
  24.       console.log($this.num1 * $this.num2)
  25.     }
  26.     multiply()
  27.  
  28.     const division = () => {
  29.       console.log(this.num2 / this.num1)
  30.     }
  31.     division()
  32.  
  33.     function subtraction(a, b) {
  34.       console.log(this, a, b)
  35.  
  36.       console.log(this.num2 - this.num1)
  37.     }
  38.     return subtraction
  39.   }
  40. }
  41. // console.log(calculator.add())
  42.  
  43. const subs = calculator.add()
  44. const result = subs.bind(calculator, 1, 2) //don't call the function , just bind the 'this'
  45.  
  46. result.apply('blabla') //don't work
  47.  
  48. console.log(result())
  49.  
  50. console.log(subs)
  51.  
  52. // console.log(subs.call(calculator, 1, 2))
  53. // console.log(subs.apply(calculator, [1, 2]))
  54.  
  55. //binding this to result
  56.  
  57. //result
  58.  
  59. //constructor function (this)
  60. //create empty object
  61. //return (this) object be default
  62. function Construct() {
  63.   this.number1 = 1
  64.   this.number = 2
  65.   // console.log(this)
  66. }
  67.  
  68. const numbersObj = new Construct()
  69. console.log(numbersObj)
  70.  
  71. //this summary
  72. //1. global context indicates window obj
  73. //2. How you call the function will define How this indicates inside this function
  74. //3.plain function call -window object
  75. //(in strict mode undefined)
  76. //array function doesn't have own this binding(parent context defines 'this')
  77. //4. calling methods as a part of object this will indicate the object
  78. //5. constructor function this indicates empty object
  79.  
  80. // const samim = {
  81. //   firstName: 'samim',
  82. //   lastName: 'hasan',
  83. //   fullName() {
  84. //     return this.firstName + this.lastName
  85. //   }
  86. // }
  87.  
  88. // const khalil = {
  89. //   firstName: 'khalil',
  90. //   lastName: 'hasan',
  91. //   fullName() {
  92. //     return this.firstName + this.lastName
  93. //   }
  94. // }
  95. //factory function
  96. // function person(firstName, lastName) {
  97. //   return {
  98. //     firstName,
  99. //     lastName,
  100. //     fullName() {
  101. //       return this.firstName + this.lastName
  102. //     }
  103. //   }
  104. // }
  105.  
  106. // const samim = person('samim', 'Hasan')
  107. // const khalil = person('Khalil', 'Hasan')
  108. // console.log(khalil)
  109.  
  110. function Person(firstName, lastName) {
  111.   this.firstName = firstName
  112.   this.lastName = lastName
  113.   this.fullName = function () {
  114.     return this.firstName + this.lastName
  115.   }
  116. }
  117. const samim = new Person('samim', 'Hasan')
  118. const khalil = new Person('Khalil', 'Hasan')
  119. console.log(samim.fullName())
  120. console.log(khalil)
  121.  
  122. //scope
  123. //var, let, const
  124. //var (global scope, functional scope)
  125. //let, const (global scope, block scope{})
  126.  
  127. //scope , scope chian
  128.  
  129. // var greet = 'Hello'
  130.  
  131. // function showGreet() {
  132. //   // var greet = 'Hola'
  133. //   // console.log(greet)
  134. //   function showHello() {
  135. //     console.log(greet)
  136. //   }
  137. //   showHello()
  138. // }
  139.  
  140. // showGreet()
  141. // console.log(greet)
  142.  
  143. // const num1 = 0
  144.  
  145. // {
  146. //   const num2 = 2
  147. //   console.log(num1)
  148. //   {
  149. //     const num3 = 3
  150. //     console.log(num3)
  151. //     console.log(num2)
  152. //   }
  153. // }
  154.  
  155. // console.log(num1)
  156. //closure (scope)
  157.  
  158. function add() {
  159.   const b = 5
  160.   const a = 3
  161.   function subtraction() {
  162.     console.log(b - a)
  163.   }
  164.   return subtraction
  165. }
  166.  
  167. const result2 = add()
  168. console.log(result2())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement