Blogger Information
Blog 49
fans 0
comment 3
visits 22926
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示用class创建类并实现各成员的声明与输出以及数组与对象的解构方法
P粉479712293
Original
524 people have browsed it

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

对应的js文件如下:

  1. // *声明一个类
  2. class Product{
  3. // *构造器初始化
  4. constructor(name,num,price){
  5. // *自有成员
  6. this.name=name;
  7. this.num=num;
  8. this.price=price;
  9. }
  10. // *共享成员(原型成员)
  11. getInfo(){
  12. return `名称:${this.name},数量:${this.num}台,单价:${this.price}元`;
  13. }
  14. // *静态成员
  15. static status="正在热卖";
  16. }
  17. // *构造器new出实例并传参:
  18. const product1=new Product('联想电脑',10,5600);
  19. console.log(product1.getInfo());
  20. console.log(Product.status);

对应的浏览器效果图如下:

题目二:实例演示数组与对象的解构方法

对应的js文件如下:

  1. // *1.数组解构
  2. // *模板=值(数组)
  3. let [name1,num,price]=['华为手机',8,5000];
  4. console.log(name1,num,price);
  5. // *更新
  6. [name1,num,price]=['华硕电脑',6,5800];
  7. console.log(name1,num,price);
  8. // *2.对象解构
  9. let{id,name2,num2,price2}={id:1,name2:'海尔空调',num2:5,price2:4500};
  10. console.log(id,name2,num2,price2);
  11. // *更新
  12. ({id,name2,num2,price2}={id:2,name2:'海信冰箱',num2:6,price2:3500});
  13. console.log(id,name2,num2,price2);

对应的浏览器效果图如下:

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