Blogger Information
Blog 38
fans 0
comment 0
visits 22650
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Javascript循环-对象字面量简化和解构赋值及应用场景
一个好人
Original
525 people have browsed it
  1. <script>
  2. // 对象字面量简化
  3. let grade1 = {
  4. // 属性
  5. code1: '48H',
  6. // 方法
  7. greet1: function(){
  8. return `${this.code1}, 开发成功`
  9. },
  10. }
  11. console.log(grade1.greet1())
  12. // 简化
  13. code2 = '52H'
  14. let grade2 = {
  15. // 属性简化
  16. code2,
  17. // 方法简化 去掉:和function
  18. greet1(){
  19. return `${this.code2}, 开发成功`
  20. },
  21. // 箭头函数,去掉function,只有一行return可以省略return和{}
  22. greet2:()=>`${grade2.code2}, 牛!`,
  23. }
  24. console.log(grade2.greet1())
  25. console.log(grade2.greet2())
  26. // 多分支
  27. // let sex = 'male'
  28. let sex
  29. if(sex=='male'){
  30. console.log('帅哥,里面请!')
  31. }else if(sex == 'female'){
  32. console.log('美女,您好!')
  33. }else{
  34. console.log('客人,请进!')
  35. }
  36. // 二分支语法糖
  37. const isVip = true
  38. console.log(isVip ? '欢迎观看':'对不起,请开通Vip')
  39. // 多分支语法糖
  40. let date = 6
  41. switch(date){
  42. case 7: console.log('今天星期天,睡个懒觉!')
  43. break;
  44. case 6: console.log('忙了一周该休息了!')
  45. break
  46. default: console.log('赶紧工作!')
  47. }
  48. // for循环
  49. let sum = 0
  50. arr = [1,2,3,4,5,6,7,8,9]
  51. for(let i = 0; i < arr.length; i++){
  52. for(let j = 0; j <= i; j++){
  53. sum += arr[i] * arr[j]
  54. }
  55. }
  56. console.log(sum)
  57. // for-of循环遍历数组value
  58. let N52H = ['nd', 'fe',' b']
  59. for(let value of N52H){
  60. console.log(value)
  61. }
  62. // 数组不能用遍历key
  63. // for(let key of N52H){
  64. // console.log(value)
  65. // }
  66. // for-in遍历对象key及value
  67. let N48H = {nd: 31, fe:68, b:1}
  68. for(let key in N48H){
  69. console.log(key)
  70. }
  71. for(let key in N48H){
  72. console.log(N48H[key]) // 不能用.语法
  73. }
  74. for(let value in N52H){
  75. console.log(value)
  76. }
  77. // forEach遍历数组
  78. N52H.forEach(item =>console.log(item))
  79. // map遍历数组,有返回值,而不是执行
  80. N54H = [4,5,6]
  81. let res = N54H.map(item =>item * 2)
  82. console.log(res)
  83. grade = ['N48H', 'N50H', 'N52H']
  84. arr = grade.map(item=>`<li><a href="">${item}</a></li>`)
  85. res = arr.join('')
  86. res = `<ul>`+res+`</ul>`
  87. console.log(res)
  88. // DOM渲染
  89. document.body.insertAdjacentHTML('afterbegin',res)
  90. // 参数不足是给默认值
  91. sum = (a, b=0)=>`${ a + b}`
  92. sum = (...arr) =>arr
  93. console.log(sum(1,2,3,4)) //不丢参数
  94. sum = (a, b,...arr) =>console.log(a,b,arr) //剩余参数压缩在arr
  95. sum = (a, b,...arr) =>console.log(a,b,...arr) //参数展开
  96. sum(9,8,7,6,5)
  97. // 求和
  98. sum = (...arr)=>{
  99. let acc = 0
  100. for(i=0;i<arr.length;i++){
  101. acc+= arr[i]
  102. }
  103. return acc
  104. }
  105. console.log(sum(9,8,7,6,5))
  106. // 返回值为对象是需要加(),不然{}容易混淆
  107. // 数组解构
  108. let [name, salary] = ['Tom', 5000]
  109. console.log(name, salary)
  110. //更新
  111. ;[name, salary] = ['Tom', 6000]
  112. console.log(name, salary)
  113. ;[name, salary, ...arr] = ['Tom', 6000, 20, 8000]
  114. console.log(name, salary, ...arr)
  115. // 对象解构
  116. let {uname:姓名, salary1} = {uname:'Tom', salary1:9000}//变量名必须与key一致
  117. console.log(姓名, salary1)
  118. // 应用场景 克隆
  119. let N35EH = {code:'52p', dy:2}
  120. let{...obj}=N35EH //变量obj就是N35EH的克隆
  121. // 解构传参
  122. let show = function(N35EH){
  123. return `${N35EH.code}:${N35EH.dy}%`
  124. }
  125. console.log(show({code:'52p', dy:3}))
  126. // 简化的对象解构传参
  127. show = function({code, dy}){
  128. return `${code}:${dy}%`
  129. }
  130. console.log(show({code:'52p', dy:4}))
  131. // 箭头简化
  132. show = ({code, dy})=> `${code}:${dy}%`
  133. console.log(show({code:'52p', dy:5}))
  134. </script>
  1. 323.html:20 48H, 开发成功
  2. 0323.html:34 52H, 开发成功
  3. 0323.html:35 52H, 牛!
  4. 0323.html:45 客人,请进!
  5. 0323.html:49 欢迎观看
  6. 0323.html:55 忙了一周该休息了!
  7. 0323.html:68 1155
  8. 0323.html:73 nd
  9. 0323.html:73 fe
  10. 0323.html:73 b
  11. 0323.html:83 nd
  12. 0323.html:83 fe
  13. 0323.html:83 b
  14. 0323.html:86 31
  15. 0323.html:86 68
  16. 0323.html:86 1
  17. 0323.html:89 0
  18. 0323.html:89 1
  19. 0323.html:89 2
  20. 0323.html:93 nd
  21. 0323.html:93 fe
  22. 0323.html:93 b
  23. 0323.html:98 (3) [8, 10, 12]
  24. 0323.html:104 <ul><li><a href="">N48H</a></li><li><a href="">N50H</a></li><li><a href="">N52H</a></li></ul>
  25. 0323.html:111 (4) [1, 2, 3, 4]
  26. 0323.html:113 9 8 7 6 5
  27. 0323.html:124 35
  28. 0323.html:129 Tom 5000
  29. 0323.html:132 Tom 6000
  30. 0323.html:134 Tom 6000 20 8000
  31. 0323.html:138 Tom 9000
  32. 0323.html:148 52p:3%
  33. 0323.html:153 52p:4%
  34. 0323.html:156 52p:5%

总结:

箭头函数挺简洁;for-of不能获取key,而for-in即可以获取key也可以获取value,不确定这个理解对不对。

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post