Blogger Information
Blog 21
fans 0
comment 0
visits 9410
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组与对象解构方法
P粉116103988
Original
680 people have browsed it

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

代码如下:

  1. <script>
  2. // 用class声明一个类 : 包括:1.公共属性,2.自有属性。 3.共享属性
  3. class user{
  4. // 公共成员:构造方法外面定义的属性
  5. username= 'php中文网';
  6. // constructor :构造方法
  7. constructor (name,email){
  8. // 自有成员:this.name: 构造方法内部定义的属性
  9. this.name= name;
  10. this.email = email;
  11. }
  12. // 共享成员:
  13. getInfo(){
  14. return `${this.name}:(${this.email})`;
  15. // 调用公共属性:
  16. // return `${thid.name}:${username}(${this.email})`;
  17. }
  18. // 静态属性:
  19. static status='enabled';
  20. }
  21. // 调用类需要new一下:
  22. const User1= new user('猪老师','zhu@php.cn');
  23. console.log(User1.getInfo());
  24. // console.log(User1.status);
  25. // 调用静态属性:
  26. console.log(user.status);
  27. </script>

效果图展示:

2. 实例演示数组与对象解构方法

代码如下:

  1. <script>
  2. // 数组结构:
  3. // 创建一个:模板=值(数组)
  4. let [username,password] = ['猪老师', 'php中文网'];
  5. console.log(username,password);
  6. // 更新数据:更新不用 let
  7. [username,password] = ['php中文网','123456'];
  8. console.log(username,password);
  9. // 对象结构:把对象里面的值放到对应的属性变量中
  10. let {user,email,sex} = {user:'猪老师',email:'php@php.cn',sex:'男'}
  11. console.log(user,email,sex);
  12. </script>

效果图展示:

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