1 回答

TA貢獻1803條經驗 獲得超6個贊
其他人在評論中解釋說您希望方法a()和b()成為實例方法而不是靜態方法,以便您可以操縱this.
為了擁有你想要的靜態鏈接,只有鏈中的第一個方法需要是靜態的。您可以調用返回實例的靜態方法create(),然后可以在該實例上調用鏈中的后續函數。這是一個簡單的例子:
class TestModel {
constructor() {
this.data = {};
}
static create() {
return new TestModel();
}
a() {
this.data.a = true;
return this;
}
b() {
this.data.b = true;
return this;
}
final() {
return this.data;
}
}
console.log(TestModel.create().a().b().final()); // => {"a": true, "b": true}
console.log(TestModel.create().a().final()); // => {"a": true}
console.log(TestModel.create().b().final()); // => {"b": true}
添加回答
舉報