Advertisement
samimwebdev

code sample, this, statement,expression (class-6)

Dec 13th, 2021
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Evolution of Function, function statement vs Expression
  2. //first class function
  3. //javascript "this"
  4.  
  5. //function statement
  6. // function sum(num1, num2){
  7. //   return num1 + num2
  8. // }
  9.  
  10. //function expression
  11. // const sum = function(num1, num2){
  12. //   return num1 + num2
  13. // }
  14.  
  15. // //arrow function
  16. // const sum = (num1, num2) => {
  17. //   return num1 + num2
  18. // }
  19.  
  20. // const sum = (num1, num2) => num1 + num2
  21.  
  22. // const sum = num1 => num1 + num1
  23.  
  24. // console.log(sum(1, 2))
  25.  
  26. //statement vs Expression
  27.  
  28. //statement means command
  29. //Expression means value
  30.  
  31. //can be written as right side of variable (expression)
  32. //can't be written as right side of variable (statement)
  33.  
  34. //first class function
  35. //criteria
  36. //can be written right side of a variable
  37. //can be passed as arguments, receive as a parameter
  38. //can be return from function
  39.  
  40. //Higher order Function function, callback
  41. //A higher order function is a function that takes a function as an argument, or returns a function
  42.  
  43. // function higherOrderFunc(fn){
  44. //    fn(1, 2)
  45. // }
  46.  
  47. // const callbackFn = function (num1, num2) {
  48. //   console.log(num1 + num2)
  49. // }
  50.  
  51. // higherOrderFunc(callbackFn)
  52.  
  53. //Higher order function
  54. // function sum(num1){
  55. //   return (num2) => {
  56. //     return num1 + num2
  57. //   }
  58. // }
  59.  
  60. // const innerFn = sum(3)
  61. // console.log(innerFn(4))
  62. // console.log(sum(3)(4))
  63.  
  64. //Javascript "this"
  65.  
  66. //this outside of function(window)
  67. //this inside of  a function
  68.   //depends on how you call the function
  69.   //if it is called in a plain way (window) strict mode(undefined)
  70.   // if you call as object reference it will indicate the object
  71.   //If you call with constructor function "this" indicates empty  object and can be fulfilled and returned
  72.  
  73. //outside of function
  74. // console.log(this)
  75.  
  76. // //inside of function
  77. // function thisFn() {
  78. //   console.log(this)
  79. // }
  80.  
  81. // thisFn()
  82.  
  83. //attaching with window object
  84. // var firstName = 'Hono'
  85. // var lastName = 'Mono'
  86.  
  87. const profile = {
  88.   firstName: 'samim',
  89.   lastName: 'Hasan',
  90.   fullName() {
  91.     const $this = this
  92.     function innerFunc() {
  93.       console.log('innerFunc', $this)
  94.     }
  95.     innerFunc() //plain way
  96.  
  97.     // innerFunc.call(profile) //plain way
  98.     return this.firstName + ' ' + this.lastName
  99.   }
  100. }
  101.  
  102. console.log(profile.fullName)
  103.  
  104. const fullNameRef = profile.fullName
  105. console.log(fullNameRef.call(profile))
  106.  
  107. // console.log(profile.fullName())
  108.  
  109. // function fullName(age1, age2) {
  110. //   return this.firstName + this.lastName + age1 + age2
  111. // }
  112.  
  113. // const obj = { firstName: 'samim', lastName: 'Hasan' }
  114.  
  115. // const result = fullName.call(obj, 27)
  116. // const result = fullName.apply(obj, [27, 30])
  117.  
  118. //like call (but do not call the function)
  119.  
  120. //for call and apply
  121. // console.log(result)
  122.  
  123. // const result = fullName.bind(obj, 30, 35)
  124. // //for bind
  125. // console.log(result())
  126.  
  127. //constructor function
  128.  
  129. // function Profile() {
  130. //   // console.log(this)
  131. //   this.firstName = 'Honu'
  132. //   this.lastName = 'Monu'
  133. //   this.fullName = function () {
  134. //     return this.firstName + this.lastName
  135. //   }
  136. // }
  137.  
  138. // const profile = new Profile()
  139. // console.log(profile)
  140. // console.log(profile.fullName())
  141.  
  142. let callArray = [0, null, '', undefined, 2, 3]
  143. function truthyValue(findtruthy) {
  144.   let arr = []
  145.   if (Array.isArray(findtruthy)) {
  146.     for (let elm of findtruthy) {
  147.       if (elm) {
  148.         arr.push(elm)
  149.       }
  150.     }
  151.   } else {
  152.     console.log('Not array')
  153.   }
  154.   return arr
  155. }
  156.  
  157. const checkIngArray = truthyValue(callArray)
  158. console.log(checkIngArray)
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement