Blogger Information
Blog 27
fans 1
comment 2
visits 90420
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS中的class类-自有,共享,静态成员的声明与输出
          
Original
906 people have browsed it

1 .Class类 自有-共享,静态成员的申明和输出

  1. // create 学生类
  2. class Stuednts {
  3. // 公共成员
  4. grade = '六年级';
  5. // 构造方法/构造函数,实例初始化
  6. constructor(name, age, add) {
  7. //自由属性
  8. this.name = name;
  9. this.age = age;
  10. this.add = add;
  11. }
  12. //***** 构造函数 和共享成员结尾不加分号';',公共成员和静态成员结尾有分号;
  13. // 共享成员: 原型成员
  14. getInof(){
  15. // 模板字面量
  16. return `姓名:${this.name} 年龄:${this.age} 地区:${this.add}`
  17. }
  18. // 静态成员
  19. static gradeID='32班';
  20. }
  21. const obj = new Stuednts('张三','15','云南')
  22. const obj2 = new Stuednts('李四','13','杭州')
  23. console.log(obj.getInof());
  24. console.log(obj.grade);
  25. console.log(Stuednts.gradeID); // 静态成员用类访问
  26. console.log(obj2.getInof());

>>>输出结果

输出结果

2 .数组和析构方法

  1. // 2.数组析构方法与对象解构
  2. // 数组解构 模板:值[数组]
  3. let [month,day]=[1,31]
  4. console.log(month+'月份有',day+'天')
  5. // 对象解构 目标是把对象中的属性值保存到对应的,与属性同名的变量中
  6. let {id,name,score} ={id:1,name:'张三',score:88}
  7. console.log(id,'姓名:'+name,'成绩:'+score);

>>>输出结果

数组析构方法与对象解构

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!