抱歉,如果我的问题不够清楚。我将把我的代码放在这里......
var chain = {
'fn_1' : {
//fn_1 code here
chain.fn_2();},
'fn_2' : {
//fn_2 code here
chain.fn_3();}
...and so on
}
假设我想调用 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();
关于javascript - 如何在没有链接的情况下调用函数链中的任何函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6256016/