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}
添加回答
举报