简体   繁体   English

是否可以将参数传递给链接的匿名函数回调?

[英]Is is possible to pass parameter to a chained anonymous function callback?

I'd like to be able to pass parameters to an anonymous callback, is this possible? 我希望能够将参数传递给匿名回调,这可能吗? See below, thus far its not looking so. 见下文,到目前为止它看起来不是这样。

var chain = {
    do: function(){
        console.log('blah');
        return this;
    },

    then: function(cb, param){
        cb.apply(param);
    }
}

chain.do().then(function(params){
    console.log(params); // undefined, expecting [1,2]
    console.log(arguments); // [], expecting [1,2]
}, [1,2]);

The first argument to apply() is the context, not the arguments. apply()的第一个参数是上下文,而不是参数。

Try... 尝试...

cb.apply(this, param);

(or whatever you want this to be). (或任何你想this是)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM