Blogger Information
Blog 35
fans 0
comment 0
visits 17007
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
class创建类和数组和类的解构
三九三伏
Original
479 people have browsed it

一、用class创建一个类, 并实现自有,共享,静态成员的声明与输出

  1. ...
  2. <script>
  3. ...
  4. class demo {
  5. //公共成员
  6. username = '小明';
  7. //构造方法
  8. constructor(name, email){
  9. //自有属性
  10. this.name = name;
  11. this.email = email;
  12. }
  13. //共享成员:原型成员
  14. getInfo(){
  15. return `${this.name}:(${this.email})`
  16. }
  17. //静态成员
  18. static status = 'enable';
  19. }
  20. const obj = new demo('晓东','xiaodong@php.cn');
  21. console.log(obj.getInfo());
  22. console.log(obj.status);
  23. console.log(demo.status);
  24. ...
  25. </script>
  26. ...

二、实例演示数组与对象解构方法

数组解构

  1. ...
  2. <script>
  3. ...
  4. const user = ['小明', 'xiaoming@php.cn'];
  5. let username = user[0];
  6. let useremail = user[1];
  7. console.log(username, useremail);
  8. console.log('------数组解构------');
  9. let [username1, useremail1]=['晓东', 'xiaodong@php.cn'];
  10. console.log(username1, useremail1);
  11. console.log('------更新值------');
  12. [username1, useremail1]=['李晓东', 'xiaodong@php.cn'];
  13. console.log(username1, useremail1);
  14. console.log('------参数不足,给默认值。------');
  15. [username1, useremail1, age = 30]=['李晓东', 'xiaodong@php.cn'];
  16. console.log(username1, useremail1, age);
  17. console.log('------参数过多,使用“...”接收------');
  18. [a, b, c, ...arr] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  19. console.log(a, b, c);
  20. console.log(arr);
  21. console.log('------解构用在数值交换------');
  22. let x = 10;
  23. let y = 20;
  24. console.log(x, y);
  25. [y, x] = [x, y];
  26. console.log(x, y);
  27. ...
  28. </script>
  29. ...

对象解构

  1. ...
  2. <script>
  3. ...
  4. let {id, lesson, score} = {id:1, lesson:'js', score:88};
  5. console.log(id, lesson, score);
  6. console.log('------更新值------');
  7. ({id, lesson, score} = {id:2, lesson:'php', score:98})
  8. console.log(id, lesson, score);
  9. console.log('------命名冲突,起别名。------');
  10. let {id:itemID, num, price} = {id:3, num:10, price:8.8};
  11. console.log(itemID, num, price);
  12. console.log('------类解构应用------');
  13. let user = {id:145, name:'牛牛', email:'niu@php.cn'};
  14. function getUser({id, name, email}){
  15. console.log(id, name, email);
  16. }
  17. getUser(user);
  18. ...
  19. </script>
  20. ...

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