Advertisement
samimwebdev

class-06(code sample)

Oct 6th, 2021
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // const arr = [1, 2, 3,[3, [5, 6]], 6, 7]
  2.  
  3. // console.log(arr[3][1][0] + arr[3][1][1])
  4.  
  5. // function sum(arr){
  6. //   let sum = 0
  7. //   const nestedArr = arr[3][1]
  8.  
  9. //   for(let num of nestedArr){
  10. //     sum += num
  11. //   }
  12. //   return sum
  13. // }
  14.  
  15. // console.log(sum(arr))
  16.  
  17. //nested object access
  18. // const obj = {
  19. //   a: 1,
  20. //   b: 2,
  21. //   c: {
  22. //     d: 1,
  23. //     e: 2,
  24. //     f: {
  25. //       g() {
  26. //         return 1
  27. //       }
  28. //     }
  29. //   }
  30. // }
  31.  
  32. // console.log(obj.c.f.g())
  33.  
  34. //first class function(criteria)
  35.  
  36. //can be assigned as variable value, passed as an arguments, returned from function
  37.  
  38. // const func1 = function (myFunc) {
  39. //   return myFunc() + 1
  40. // }
  41.  
  42. // const func2 = function(){
  43. //   return 2
  44. // }
  45. // console.log(func1(func2))
  46.  
  47. // function sum(number1) {
  48. //   return function(number2){
  49. //     return number1 + number2
  50. //   }
  51. // }
  52. // console.log(sum(4)(3))
  53.  
  54. // console.log(sum(3)(4))
  55.  
  56. //default parameter
  57.  
  58. // function multiply(num1 = 10, num2 = 50, num3 = 7) {
  59. //   console.log(num1, num2, num3)
  60. //   console.log(num1 * num2 * num3)
  61. // }
  62.  
  63. // multiply(20, null, 5)
  64.  
  65.  
  66. // const num1 = 10
  67. // function printNum (num){
  68. //   // console.log(num)
  69. //   return num
  70. // }
  71. // // printNum(num1)
  72. // console.log(printNum(num1))
  73.  
  74.  
  75. //array destructuring
  76. // const myArr = [1, 2, 3]
  77.  
  78. // const num1 = myArr[0]
  79. // const num2 = myArr[1]
  80.  
  81. //destructuring
  82. // const [num1,,num3] = myArr
  83.  
  84. // console.log(num1, num3)
  85.  
  86. //object destructuring
  87.  
  88. // const myObj = {
  89. //   a: 1,
  90. //   b: 2,
  91. //   c: [3, 4]
  92. // }
  93.  
  94.  
  95. // const val1 = myObj.a
  96. // const val2 = myObj.b
  97. // const val3 = myObj.c.d
  98.  
  99. // const {b: val2, a: val1, c: [ran1, ran2]} = myObj
  100.  
  101. // console.log(val2, val1, ran1, ran2)
  102.  
  103. // console.log(val1, val2, val3)
  104.  
  105. //array spreading and rest
  106. const myArr = [1, 2, 3]
  107. //spread
  108. const newArr = [...myArr, 4]
  109.  
  110. //rest
  111. const  [num1, ...restValues] = newArr
  112.  
  113. console.log(num1, restValues)
  114.  
  115.  
  116.  
  117. const myObj = {
  118.   a: 1,
  119.   b: 2,
  120.   c: [3, 4]
  121. }
  122.  
  123.  
  124.  
  125.  
  126. //spread
  127. const newObj = {
  128.   ...myObj,
  129.   d: 5
  130. }
  131. console.log(newObj)
  132.  
  133. //rest
  134. const {a, ...rest} = newObj
  135.  
  136. console.log(a, rest)
  137.  
  138.  
  139.  
  140.  
  141. //1.data naming issue
  142. //2. order issue
  143.  
  144. function getProfileInfo({age, firstName, lastName}){
  145.   console.log(`${firstName} ${lastName}-${age}`)
  146. }
  147.  
  148. //special case
  149. function getProfileInfo(...profile) {
  150.   console.log(profile)
  151.   console.log(`${profile[0].firstName} ${profile[0].lastName} - ${profile[0].age }`)
  152.   // console.log(`${firstName} ${lastName}-${age}`)
  153. }
  154.  
  155. getProfileInfo({
  156.   firstName: 'samim',
  157.   lastName:  'Hasan',
  158.   age: 28
  159. })
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement