ES6引入Class类这个概念,类似java语言中的class类,通过class关键字,可以自定义;通过new关键字实例化,类的方法名可以采用表达式
let methodName = 'getAdd';
class Point {
constructor(x,y){
this.x = x;
this.y = y;
}
count() {
return this.x*this.y;
}
[methodName](){
return this.x + this.y;
}
}
p = new Point(2,6)
p.count()//12
p.getAdd()//8
//类的方法是定义在prototype上的,可以通过assign随时添加若干个
Object.assign(Point.prototype, {
method_01(){},
methos_02(){}
});