ES6系列-类与对象

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
// 类与对象
{
class Person {
constructor(name = 'yoyo') {
this.name = name;
}
}
let v_person = new Person('v')
console.log(v_person) //Person {name: "v"}

class Child extends Person {
constructor(name = 'child') {
// super()会使用父类的所有
super(name)
this.type = 'dog'

}
}
console.log(new Child()) //Child {name: "child", type: "dog"}
console.log(new Child('hello')) //Child {name: "hello", type: "dog"}

}
// getter,setter
{
class Person {
constructor(name = 'yoy') {
this.name = name
}
get longName() {
return this.name + '读取属性'
}
set longName(value) {
this.name = value
}
}
let v = new Person()
console.log(v.longName, 'getter')//yoy读取属性 getter
v.longName = 'hello'
console.log(v.longName) //hello读取属性
let vs = new Person('lele')
console.log(vs.longName, 'setter') //lele读取属性 setter


}


{
// 静态方法,不是使用类的实例去调用
class Person {
constructor(name = 'yoyo') {
this.name = name
}
static tail() {
console.log('this is tail')
}
}
Person.tail()
}

{
// 静态属性 ===> 直接在类上定义
class Person {
constructor(name = 'uouo') {
this.name = name
}
static tail() {
console.log('this is tail')
}
}
Person.numberId = 23333
console.log(Person.numberId) //2333

}