我记得曾经看到过一种快捷方式,可以不用在构造函数中进行this.foo赋值,只要属性名和构造函数参数名相同 - 但是我在谷歌上找不到相关的信息了。
例如:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
您是否可以尝试这样做:
class Polygon {
constructor(height=height, width=width) {
// wasn't there a way to declare these arguments so it auto sets the instance variables?
}
}