Blogger Information
Blog 41
fans 2
comment 0
visits 29356
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js之数组对象与访问器
月光下,遗忘黑暗
Original
502 people have browsed it

1.数组对象

  • 代码块
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <script>
    9. let object = {
    10. name : '123',
    11. sex : '男'
    12. }
    13. let arr = [1,2,3];
    14. object.name = 321;
    15. console.table([object]);
    16. console.table(arr);
    17. </script>
    18. </body>
    19. </html>
  • 效果图

2.传参解构

  • 数组解构

    代码块

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <script>
    9. let arr = [1,2,3,4,5];
    10. let [,,a,b,c,,] = arr;
    11. let fun = (a,b,c) => {
    12. console.table([a,b,c]);
    13. }
    14. fun(a,b,c);
    15. [a,c] = [c,a];
    16. console.table([a,c]);
    17. </script>
    18. </body>
    19. </html>

    效果图

  • 对象解构

    代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>对象解构</title>
  6. </head>
  7. <body>
  8. <script>
  9. let obj = {id : 10, name : '手机'};
  10. ({id,name} = obj);
  11. console.log(id,name);
  12. let getUser = ({id,name}) => [name,id];
  13. console.log(getUser({id,name}));
  14. </script>
  15. </body>
  16. </html>

效果图

3.访问器属性的set和get

代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>访问器属性</title>
  6. </head>
  7. <body>
  8. <script>
  9. const product = {
  10. data:[
  11. {id:1,name:'电脑',price:5000,num:5}
  12. ],
  13. get total(){
  14. return this.data.reduce((t,c) => (t += c.price * c.num),0);
  15. }
  16. }
  17. console.log(product.total);
  18. let user = {
  19. data : {name},
  20. // 读
  21. get name() {
  22. return this.data.name;
  23. },
  24. // 写
  25. set name(v) {
  26. this.data.name = v;
  27. },
  28. }
  29. user.name = '手机';
  30. console.log(user.name)
  31. </script>
  32. </body>
  33. </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