es6(12)--类,对象

 1 //类,对象
 2 {
 3     //基本定义和生成实例
 4     class Parent{
 5         //定义构造函数
 6         constructor(name='QQQ'){
 7             this.name=name;
 8         }
 9     }
10     let v_parent=new Parent('v');
11     console.log(v_parent);
12 }
13 
14 {
15     //继承
16     class Parent{
17         //定义构造函数
18         constructor(name='QQQ'){
19             this.name=name;
20         }
21     }
22     class Child extends Parent{
23 
24     }
25     console.log('继承',new Child())
26 }
27 {
28 
29     //继承传递参数
30     class Parent{
31         //定义构造函数
32         constructor(name='QQQ'){
33             this.name=name;
34         }
35     }
36     class Child extends Parent{
37         constructor(name='child'){
38             super(name);//参数为空则会用父类的,需要覆盖父类,就要有参数
39             this.type='child';//super要放在第一行
40         }
41     }
42     console.log('继承传递参数',new Child('he'))
43 
44 }
45 {
46     //getter,setter
47     class Parent{
48         constructor(name='QQQ'){
49             this.name=name;
50         }
51         get longName(){
52             return 'mk'+this.name
53         }
54         set longName(value){
55             this.name=value;
56         }
57     }
58     let v=new Parent();
59     v.longName="hekk"
60     console.log('getter',v.longName);
61 }
62 {
63     //静态方法
64     class Parent{
65         constructor(name='QQQ'){
66             this.name=name;
67         }
68         //通过类去调用
69         static tell(){
70             console.log('tell')
71         }
72     }
73 
74     Parent.tell();
75 
76 }
77 {
78     //静态属性
79     class Parent{
80         constructor(name='QQQ'){
81             this.name=name;
82         }
83         //通过类去调用
84         static tell(){
85             console.log('tell')
86         }
87     }
88     Parent.type='test';
89     console.log(Parent.type)
90 }
原文地址:https://www.cnblogs.com/chenlw/p/9227911.html