
- 可在方法中用this.constructor.prototype挂在额外方法
- this.constructor指向 class Point {…} 所以在this.constructor.prototype上可以挂载方法。
- 追加的方法内部同样可以调用constructor构造函数声明的属性
- 也可以使用Object.assign 动态追加方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23{
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
add() {
this.constructor.prototype.add1 = () => {
console.log('这是追加的add1方法' + this.x)
}
Object.assign(Point.prototype,{
add2() {
console.log('这是追加的add2方法')
}
})
}
}
var a = new Point(2, 3)
a.add();
a.add1();
a.add2();
}




近期评论