ham*_*ama 0 javascript function object literals chain
对不起,如果我的问题不够明确.我会把我的代码放在这里......
var chain = {
'fn_1' : {
//fn_1 code here
chain.fn_2();},
'fn_2' : {
//fn_2 code here
chain.fn_3();}
...and so on
}
Run Code Online (Sandbox Code Playgroud)
让我们说如果我调用chain.fn_1(),有没有办法可以在不调用chain.fn_2()的情况下执行此操作?
我现在能想到的是一面旗帜,但每个功能可能会有很多过剩的旗帜.你们有什么想法吗?
如果一系列函数都调用下一个函数你是正确的,那么你需要有一些标志.很有可能,最好的方法是修改函数,使它们返回对象的引用.然后你可以像这样链:
var chain = {
'fn_1': function () {
// do something here.
return this;
},
'fn_2': function () {
// do something here.
return this;
},
'fn_3': function () {
// do something here.
return this;
}
};
// call the full chain:
chain.fn_1().fn_2().fn_3();
// call only the middle.
chain.fn_2();
Run Code Online (Sandbox Code Playgroud)