我见过的类模式几乎是这样的:
class Foo {
constructor(x, y, z) {
this._x = x;
this._y = y;
this._z = z;
}
get x() {
return this._x;
}
set x(value) {
//I acctually do some stuff here
this._x = value;
}
get y() {
return this._y;
}
set y(value) {
//I acctually do some stuff here
this._y = value;
}
get z() {
return this._z;
}
set z(value) {
//I acctually do some stuff here
this._z = value;
}
}
console.log(new Foo('x', 'y', 'z'))
执行输出:
Foo { _x: 'x', _y: 'y', _z: 'z' }
console.log(JSON.stringify(new Foo('x', 'y', 'z')))
执行输出:
{"_x":"x","_y":"y","_z":"z"}
这给了我下划线前缀的字段,而我的目标不是那个,我怎样才能让字段没有下划线前缀,但是,让 getter 和 setter 由instance.prop
交互触发。