类

  • 基本语法
  • 类的继承
  • 静态方法
  • 静态属性
  • getter
  • setter
{
    // 基本定义和生成实例
    class Parent{
        constructor(name='mukewang'){
          this.name=name;
        }
    }
    let v_parent=new Parent('v');
    console.log('构造函数和实例',v_parent);
    //  Person {name:"v"}
}
{
  // 继承
  class Parent{
    constructor(name='mukewang'){
      this.name=name;
    }
  }

  class Child extends Parent{

  }

  console.log('继承',new Child());
  // _Child {name:"mukewang"}
}

{
  // 继承传递参数
  class Parent{
    constructor(name='mukewang'){
      this.name=name;
    }
  }

  class Child extends Parent{
    constructor(name='child'){
      super(name);
      this.type='child';
      // 定义自己的属性,this一定是在super之后
    }
  }

  console.log('继承传递参数',new Child('hello'));
  // _Child{name:"hello",type:"child"}
}

{
  // getter,setter
  class Parent{
    constructor(name='mukewang'){
      this.name=name;
    }
    // longName是属性,不是函数
    get longName(){
      return 'mk'+this.name
    }

    set longName(value){
      this.name=value;
    }
  }

  let v=new Parent();
  console.log('getter',v.longName); // mkmukewang
  v.longName='hello';// set 操作
  console.log('setter',v.longName);// mkhello
}

{
  // 静态方法
  class Parent{
    constructor(name='mukewang'){
      this.name=name;
    }
    // static只是定义静态方法
    static tell(){
      console.log('tell');
    }
  }

  Parent.tell();

}

{
  // 静态属性
  class Parent{
    constructor(name='mukewang'){
      this.name=name;
    }

    static tell(){
      console.log('tell');
    }
  }

  //定义静态属性
  Parent.type='test';

  console.log('静态属性',Parent.type);


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105