-1

假设我有这个作为 jQuery 小部件的选项:

    function oneFunc()
    {
     var myVar;

       //there is some widget calling
       $.widget("ui.combobox", $.ui.autocomplete, {
                options: {
                       source: function (request, response){////doing something with myVar, request and response}
                }
       });
    }

现在我想分离出function (request, response)使用回调

所以,我想要这样的东西:

function oneFunc()
{
     var myVar;
     //there is some widget calling
        $.widget("ui.combobox", $.ui.autocomplete, {
                options: {
                       source: myCallBack
       });
}

function myCallBack(request, response){
//I can get request and response here by default but not myVar
//doing something with myVar, request and response
}

所以,我无法访问 myVar。我必须通过那里。但是怎么做呢?

编辑: 我不想使用全局变量 requestresponse无论如何我都可以在 myCallBack 中获得默认值。

如果可以避免匿名函数,那就更好了。

4

3 回答 3

2

If you want to access myVar inside your separated callback function, I would make it explicit in the declaration:

function myCallBack(request, response, myVar) 
{
}

This makes it easier to keep track of when you see it in your code later on. Then, you write a proxy function like this:

source: function(request, response) {
    return myCallBack.call(this, request, response, myVar);
}

If you want a more complex scope or myVar needs to be changed in both scopes, you need an object:

var myScope = {
    myVar: null
};

// ...

source: function(request, response) {
    return myCallBack.call(this, request, response, myScope);
}

Then, inside the callback:

function myCallBack(request, response, myScope) 
{
    // use myVar as myScope.myVar
}
于 2013-07-08T12:14:22.787 回答
2

您可以通过使用Function.applyFunction.call

function oneFunc(myCallback)
{
     this.myVar = 1;
    var request = "request";
    var response = "response"
     //there is some widget calling
     myCallback.apply(this,[request,response]);
}

function callback(request, response){
   console.log(request);
    console.log(response);
    console.log(this.myVar);
}

oneFunc(callback);

以上输出

request
response
1

因为您已将关键字委托this给回调方法,从而允许它访问在原始方法中声明的任何变量。

现场示例:http: //jsfiddle.net/hFsCA/

请注意,该apply行也可以替换为(感谢@AlessandroVendruscolo)

myCallback.call(this,request,response);

并不是说它有太大的不同——而是为了完整性!

因此,将其包装回您的(现已更新)示例:

function oneFunc(callback)
{
   this.myVar = 1;
   var self = this;
   //there is some widget calling
   $.widget("ui.combobox", $.ui.autocomplete, {

            options: {
                 source: function (request, response){
                        callback.call(self,request,response);
                 }
            }
   });

}
于 2013-07-08T11:50:31.447 回答
0

我不知道 jQuery 是否在内部使用任何匿名函数。但我通过这个解决了它:

function oneFunc()
{
     var myVar;
     //there is some widget calling
        $.widget("ui.combobox", $.ui.autocomplete, {
                options: {
                       source: $.proxy(myCallBack, this, myVar)
       });
}

function myCallBack(myVar, request, response){
//I can access myVar, request and response
//doing something with myVar, request and response
}

我猜其他有经验的人可以对此发表评论。

于 2013-07-09T04:59:08.817 回答