Blogger Information
Blog 65
fans 2
comment 0
visits 60247
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
ES6对象与数组解构,我滴哥!这样玩!代码量减少60%...
张福根一修品牌运营
Original
750 people have browsed it

实例演示对象与数组的解构

实例展示:

对象与数组的解构

实例源码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>实例演示对象与数组的解构</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 1. 对象解构
  11. const xiangmu = {
  12. name: "卫生间翻新",
  13. price: 8999,
  14. area: "成都",
  15. };
  16. // 声明三个变量
  17. let name, price, area;
  18. name = "卫生间翻新";
  19. price = 8999;
  20. area = "成都";
  21. console.log(name, price, area);
  22. // 完全解构
  23. ({ name, price, area } = xiangmu);
  24. console.log(name, price, area);
  25. // 不完全解构
  26. ({ name, price, area, shifu } = xiangmu);
  27. console.log(name, price, area, shifu);
  28. // 默认值
  29. ({ name, price, area, shifu = "张三" } = xiangmu);
  30. console.log(name, price, area, shifu);
  31. // =======================================================================
  32. // 2. 数组解构
  33. const areas = ["成都", "北京", "上海", "深圳", "广州"];
  34. let [张师傅, 李师傅, 王师傅, 马师傅, 周师傅] = areas;
  35. console.log(张师傅, 李师傅, 王师傅, 马师傅, 周师傅);
  36. // 完全解构
  37. let [a, b, c, d] = ["张", "李", "王", "马"];
  38. console.log(a, b, c, d);
  39. // 不完全解构
  40. [a, ...b] = ["张", "李", "王", "马"];
  41. console.log(a, b);
  42. // 未定义:
  43. [a, b, c, d] = ["张", "李", "王"];
  44. console.log(a, b, c, d);
  45. // 默认值:
  46. [a, b, c, d = "马师傅"] = ["张", "李", "王"];
  47. console.log(a, b, c, d);
  48. </script>
  49. </body>
  50. </html>

实例总结:

  • 解构主要针对集合类型有意义,对象,数组
  • 对象解构的语法形式是在一个赋值操作左边放置一个对象字面量
  • 数组解构,使用的是数组字面量,且解构操作全部在数组内完成
Correcting teacher:天蓬老师天蓬老师

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