Advertisement
samimwebdev

class-1(Essential JavaScript for react)

Feb 21st, 2022
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //var,  const let
  2. //function declaration, arrow function and single line return
  3. //template string/literal
  4. //conditional and logical AND(&&)
  5. //Array helper method(map, filter, find)
  6. // Destructuring, rest, spread
  7. // promise, async await
  8. // class
  9.  
  10. //Let, var, const
  11. // var a = 10 //Don't use(to avoid some unexpected  error, bug)
  12.  
  13. // const b = 20
  14. // let c = 30
  15.  
  16. // // b = 40 //error
  17. // c = 50
  18.  
  19. // console.log(c)
  20.  
  21. //function statement
  22. // function sum(num1, num2){
  23. //    return num1 + num2
  24. // }
  25.  
  26. //function expression
  27. // const sum = function (num1, num2) {
  28. //   return num1 + num2
  29. // }
  30.  
  31. //arrow function
  32. // const sum = (num1, num2) => {
  33. //     return num1 + num2
  34. //   }
  35.  
  36. // const sum = (num1, num2) => num1 + num2
  37.  
  38. // const multiply = (num) => num * 20
  39.  
  40. // console.log(multiply(10))
  41.  
  42. // const firstName = 'samim'
  43. // const lastName = 'Hasan'
  44.  
  45. // const fullName = firstName + ' ' + lastName
  46. //javascript expression can be written inside in curly braces
  47. //statement - command (;) expression-value
  48. // const fullName = `My First Name is "${firstName.toUpperCase()}" and last name is "${lastName.toUpperCase()}".`
  49.  
  50. // console.log(fullName)
  51.  
  52. //conditional
  53.  
  54. // const age = 20
  55.  
  56. // if (age >= 18) {
  57. //   console.log('You can vote')
  58. // } else {
  59. //   console.log("You can't vote")
  60. // }
  61.  
  62. //ternary
  63. // condition? true : false
  64.  
  65. // const result = age >= 18 ? 'You can vote' : 'You can\'t vote'
  66.  
  67. //logical and operator(&&)
  68. // const result = age >= 18 && 'You can vote'
  69.  
  70. // const result = age >= 18 || 'You can vote'
  71.  
  72. // console.log(result)
  73.  
  74. // function double(nums) {
  75. //   const dbl = []
  76. //imperative coding
  77. //   for (let i = 0; i < nums.length; i++) {
  78. //     dbl.push(nums[i] * 2)
  79. //   }
  80. //declarative
  81. //   for (let num of nums) {
  82. //     dbl.push(num * 2)
  83. //   }
  84. //   return dbl
  85. //declarative
  86. //   const result = nums.map((num) => num * 2)
  87. //   return result
  88. // }
  89.  
  90. // const nums = [1, 2, 3, 4]
  91. // console.log(double(nums))
  92.  
  93. //[2, 4, 6, 8]
  94.  
  95. //find
  96. // function findElm(recArr) {
  97. //   return recArr.find((elm) => elm > 2)
  98. // }
  99. // const nums = [1, 2, 3, 4]
  100. // console.log(findElm(nums))
  101.  
  102. //filter
  103.  
  104. // function filterElms(recArr) {
  105. //     return recArr.filter((elm) => elm > 2)
  106. //   }
  107. //   const nums = [1, 2, 3, 4]
  108. //   console.log(filterElms(nums))
  109.  
  110. //destructuring,
  111. // const person = {
  112. //   firstName: 'samim',
  113. //   lastName: 'Hasan',
  114. //   age: 30,
  115. // }
  116.  
  117. // const { firstName, age } = person
  118.  
  119. // console.log(firstName)
  120. // console.log(age)
  121.  
  122. // rest operator
  123. // const { firstName, ...restValues } = person
  124. // console.log(firstName)
  125. // console.log(restValues)
  126.  
  127. // // spread operator
  128. // const newPersonObj = {
  129. //     ...person,
  130. //     profession: 'Programmer'
  131. // }
  132.  
  133. // console.log(newPersonObj)
  134.  
  135. // //array destrucring, rest, spread operator
  136. // const arr = [1, 2, 3, 4]
  137. // const [num1, num2, ...restArr] = arr
  138. // console.log(num1, num2)
  139. // console.log(restArr)
  140. // const newArr = [...arr, 5]
  141. // console.log(newArr)
  142. // console.log(arr)
  143.  
  144. // function viewPerson({ firstName, lastName, age }) {
  145. //   return firstName, lastName, age
  146. // }
  147.  
  148. // const person = {
  149. //   firstName: 'samim',
  150. //   lastName: 'Hasan',
  151. //   age: 30,
  152. // }
  153.  
  154. // console.log(viewPerson(person))
  155.  
  156. //callback
  157.  
  158. //promise
  159. // fetch('https://jsonplaceholder.typicode.com/todos/1')
  160. //   .then((response) => response.json())
  161. //   .then((json) => console.log(json))
  162. //   .catch(err => console.log(err))
  163.  
  164. //async await
  165. // async function run(){
  166. //     try{
  167. //         const res = await fetch('https://jsonplaceholder.typicode.com/todos/1')
  168. //         const data = await res.json()
  169. //         console.log(data)
  170. //     }catch(err){
  171. //         console.log(err)
  172. //     }
  173.  
  174. // }
  175.  
  176. // run()
  177.  
  178. class Person {
  179.   //filed/property
  180.   // name = 'samim'
  181.   // age = 30
  182.   constructor(mName, mAge) {
  183.     console.log(mName, mAge)
  184.     this.name = mName
  185.     this.age = mAge
  186.   }
  187.   //method
  188.   showInfo() {
  189.     return this.name + '-' + this.age
  190.   }
  191. }
  192.  
  193. // const person = new Person('khalil', 35)
  194. // console.log(person)
  195. // console.log(person.name)
  196. // console.log(person.age)
  197. // console.log(person.showInfo())
  198. //subclass(child class) and inheritance
  199. class Student extends Person {
  200.   constructor(name, age, course) {
  201.     super(name, age)
  202.     this.course = course
  203.   }
  204.  
  205.   showCoursesInfo() {
  206.     return super.showInfo() + ' ' + this.course
  207.   }
  208. }
  209.  
  210. const student = new Student('anis', 25, 'React Student')
  211. console.log(student)
  212. console.log(student.showCoursesInfo())
  213. console.log(student)
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement