【问题标题】:javascript call array of functions with the same contextjavascript调用具有相同上下文的函数数组
【发布时间】:2016-12-14 14:52:20
【问题描述】:

我正在尝试创建回调功能,或者至少我是这样命名的。主要目的是从具有相同上下文的数组中调用函数,并能够从当前执行的函数中调用数组中的下一个函数。

这是我想出的代码:

function callCallbackChain(context, callbackChain, params) {
    callbackChain.push(function () {
        return true;
    });
    for (var i = 0, len = callbackChain.length - 1; i < len; i++) {
        var cb   = callbackChain[i];
        var next = callbackChain[i + 1];
        if (typeof cb === "function") {
            cb.apply(context, [next].concat(params));
        }
    }
}
var callbackChain = [
    function (next, foo) {
        console.log('1 function call', arguments);
        this.innerHTML = this.innerHTML + "function called + context:" + foo + "<br>";
        next()
    },
    function (next, foo) {
        console.log('2 function call', arguments);
        this.innerHTML = this.innerHTML + "function called + context:" + foo + "<br>";
        next()
    },
    function (next, foo) {
        console.log('3 function call', arguments);
        this.innerHTML = this.innerHTML + "function called + context:" + foo + "<br>";
        next()
    }
];


var d = document.querySelector("#conent");
callCallbackChain(d, callbackChain, ["param from the main function"]);
<div id="conent">
  Initial Content
  
</div>

我似乎无法正确设置next 功能。它有点像中间件。

【问题讨论】:

    标签: javascript arrays middleware


    【解决方案1】:

    您的 next 函数实际上不能是链中的下一个函数。

    它的意图是运行下一个函数。

    function run(context, chain, params) {
      var inner_run = (context, chain, params, index) => {
        next = () => inner_run(context, chain, params, index + 1);
        if (index < chain.length) chain[index].apply(context, [next].concat(params));
      };
      inner_run(context, chain, params, 0);
    }
    
    var chain = [
      function (next, foo) {
        this.first = true
        console.log('first', arguments);
        next()
      },
      function (next, foo) {
        this.second = true
        console.log('second', arguments);
        // not calling next()
      },
      function (next, foo) {
        this.third = true
        console.log('third', arguments);
        next()
      }
    ];
    
    var context = {};
    
    run(context, chain, ["param"]);
    
    console.log('context', context);

    【讨论】:

    • 这个确实对我有用。我已经完成了我的版本,但这是完整的工作答案,所以我会接受它。
    【解决方案2】:

    您需要即时绑定它:

    var nextelem=0;
    function next(params...){
     temp=nextelem;
     nextelem++;
     callbackChain[temp].bind(context)(next,...params);
    }
    //pass it
    nextelem=1;
    callbackChain[0].bind(context)(next);
    

    【讨论】:

    • 我仍然收到undefined 上下文jsfiddle.net/xr8q7s1f/2
    • 下一个函数需要是递归的,所以你需要传递一个非常复杂的函数。动态生成它应该更容易......
    • 我已经做到了jsfiddle.net/xr8q7s1f/4现在我会试试你的版本
    • 好吧,代码没有按预期工作,但现在已修复jsfiddle.net/xr8q7s1f/5
    • 是的,它只是伪代码,没有写完整的答案
    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多