Advertisement
samimwebdev

class-6 Javascript "this" and first class function

Feb 24th, 2022
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //javascript 'this', call bind apply
  2.  
  3. //"this"
  4. //global execution context
  5. //functional execution context
  6.  
  7. // const a = 10
  8.  
  9. // function sum(num1, num2) {
  10. //   return num1 + num2
  11. // }
  12.  
  13. // console.log(sum(10, 15))
  14. // console.log(a)
  15.  
  16. //in global execution context - window
  17. // In functional execution context
  18. //- plain function call - window, undefined(in strict mode)
  19. //- with object chaining- that object
  20. // inside constructor function - empty object
  21. //in strict mode (plain function call) - undefined
  22.  
  23. // console.log(this) // window
  24.  
  25. // function greet() {
  26. //   console.log(this) //window
  27. //   return 'Hi'
  28. // }
  29. // //how you call the function
  30. // //plain way - window
  31. // greet()
  32.  
  33. // const person = {
  34. //   firstName: 'samim',
  35. //   lastName: 'Fazlu',
  36. //   fullName() {
  37. //     console.log(this)
  38. //     // const $that = this
  39.  
  40. //     // function inner() {
  41. //     //   console.log($that)
  42. //     //   return $that.firstName + ' ' + $that.lastName
  43. //     // }
  44. //     //arrow function has no this binding
  45. //     const inner = () => {
  46. //       return this.firstName + ' ' + this.lastName
  47. //     }
  48. //     console.log(inner())
  49. //   },
  50. // }
  51.  
  52. // person.fullName()
  53. // const fullName = person.fullName
  54. // console.log(fullName.call(person))
  55.  
  56. // function Person(){
  57. //     //this = {}
  58.  
  59. //  this.fName = 'samim'
  60. //  console.log(this)
  61. // }
  62.  
  63. // const samim = new Person()
  64. // console.log(samim)
  65.  
  66. // function greet(lName, age) {
  67. //   return 'Hi' + ' ' + this.name + ' ' + lName + '-' + age
  68. // }
  69. // //you can define 'this'
  70. // const person = { name: 'samim' }
  71. // // const obj = greet.call(person, 'Hasan', 30)
  72. // // const obj = greet.apply(person, ['Hasan', 30])
  73. // //work like call , function is not called
  74. // const boundFunc = greet.bind(person, 'Hasan', 30)
  75. // //this can't be changed (after bind once)
  76. // console.log(boundFunc())
  77.  
  78. //first class function
  79.  
  80. //function(value) can be used as any data type
  81.  
  82. //functions can be assigned to any other variable or passed as an argument or can be returned by another function
  83.  
  84. // A “higher-order function” is a function that accepts functions as parameters and/or returns a function.
  85. //greet -higher order function
  86. //func - callback function
  87. // const greet = (fn) => {
  88. //    fn('Hi')
  89. //   //   console.log('Hi')
  90. // }
  91.  
  92. // const func = function (initial) {
  93. //     console.log(initial + ' ' + 'world')
  94. //   }
  95.  
  96. // console.log(
  97. //   greet(func)
  98. // )
  99.  
  100. //Higher order function
  101. //lambda function (innerFunc)
  102. function sum(num1) {
  103.   return (num2) => {
  104.     return num1 + num2
  105.   }
  106. }
  107.  
  108. // const secondFunc = sum(3)
  109. // console.log(secondFunc)
  110. // console.log(secondFunc(2) )
  111. // console.log(sum(3)(2))
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement