看一下构造函数中如何声明x和y:
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '(' + this.x + ', ' + this.y + ')';
}
}
有没有一种方法可以在函数之外声明属性,例如:
class Point {
// Declare static class property here
// a: 22
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '(' + this.x + ', ' + this.y + ')';
}
}
所以我想把a赋值为22,但我不确定是否可以在构造函数之外但仍在类内部完成。
static get x(){ return 5; }
,这样做的效果是一样的,只是有点啰嗦。 - somethinghere