假设我有这个作为 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。我必须通过那里。但是怎么做呢?
编辑:
我不想使用全局变量
request
,response
无论如何我都可以在 myCallBack 中获得默认值。
如果可以避免匿名函数,那就更好了。