Blogger Information
Blog 17
fans 1
comment 0
visits 8838
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js之Class构造函数
P粉933302309
Original
834 people have browsed it

1.用class实现自有成员,共享成员,静态成员的声明和输出!

  1. class US2 {
  2. // 构造方法,初始化
  3. constructor(name, num) {
  4. // 自有成员
  5. this.name = name;
  6. this.num = num;
  7. }
  8. // 共享成员
  9. getInfo() {
  10. return `${this.name} : (${this.num})`;
  11. }
  12. // 静态成员
  13. static static = "6666";
  14. }
  15. // 自有实例
  16. let us1 = new US2("老王111", 10);
  17. let us2 = new US2("老王222", 10);
  18. console.log(us1, us2);
  19. // 共享实例
  20. console.log(us1.getInfo(), us2.getInfo());
  21. // 静态实例
  22. console.log(US2.static);

2.数组和对象的解构方法

1.数组解构

  1. let [aa, bb] = ["你好啊", "我很好"];
  2. console.log(aa, bb);
  3. // 更新
  4. [aa, bb] = ["hello", "world"];
  5. console.log(aa, bb);
  6. // 参数不足:给默认值
  7. [aa, bb, cc = "笨蛋"] = ["hello", "world"];
  8. console.log(aa, bb, cc);
  9. // 参数过多:...rest
  10. [a, b, ...arr] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
  11. console.log(a, b);
  12. console.log("----------------");
  13. console.log(arr);
  14. console.log("----------------");
  15. console.log(a, b, ...arr);

2.对象结构

  1. let { id, jg, num } = { id: 1, jg: 2000, num: 3 };
  2. console.log(id, jg, num);
  3. // 更新
  4. ({ id, jg, num } = { id: 100, jg: 5000, num: 20 });
  5. console.log(id, jg, num);
  6. // 命名冲突,起别名
  7. ({ id: id2, jg1, num1 } = { id: 10, jg1: 500, num1: 200 });
  8. console.log(id2, jg1, num1);
  9. // 演示案例
  10. let user = { id: 1, jg: 880, num: 3 };
  11. function getUser({ id, jg, num }) {
  12. console.log(id, jg, num);
  13. }
  14. getUser(user);
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