我有这个对象:
const Foo = {
bar(x) {
},
baz(x) {
},
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能重写这个对象,以便能够像这样调用它的方法:
Foo.bar(x).baz(y);
Run Code Online (Sandbox Code Playgroud)
而不是像这样调用它们:
Foo.bar(x);
Foo.baz(y);
Run Code Online (Sandbox Code Playgroud)
就像jQuery你可以在哪里调用一个接一个的功能......
$('bac').addClass('x').removeClass('y');
你将不得不使用每个函数返回对象本身,因此你可以再次调用它上面的函数(这也是jQuery的作用).
const Foo = {
bar(x) {
return this;
},
baz(x) {
return this;
},
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |