1

我目前在 javascript ajax 调用中有一个无名函数:

(此代码已精简到要领,可能存在一些错误)

function call(name){
    var type = services[name].type


    $.oajax({
        success: function(data) {
            fbposts=data.data
                if (type === "grupp"){
                    var postid=fbposts[fbpost].id.split("_");
                    return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/'
                }
                else if (fbposts[fbpost].actions){
                    return fbposts[fbpost].actions[0].link;
                }
            }
        }       
    })
};

我想在欧洲使用

success: successfunction,

并引用这样的函数:

function success(data) {
            fbposts=data.data
                if (type === "grupp"){
                    var postid=fbposts[fbpost].id.split("_");
                    return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/'
                }
                else if (fbposts[fbpost].actions){
                    return fbposts[fbpost].actions[0].link;
                }
            }
        }       
    })

此时,不再定义类型变量。我能以某种方式解决这个问题吗?

4

3 回答 3

3

一种可行的方法是使用context参数 on $.ajax-- 例如:

function successFunction(data) {
    fbposts=data.data;
    if (type === "grupp"){
        var postid=fbposts[fbpost].id.split("_");
        return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/'
    }
    else if (fbposts[fbpost].actions){
        return fbposts[fbpost].actions[0].link;
    }
}

function call(name){
    var type = services[name].type;
    $.oajax({
        success: successFunction,
        context: { type: type }
    );
}

在你的函数中使用contextcause指向你传入的任何内容。所以在上面的例子中,当我们阅读thissuccesscontextsuccessFunction

if (type ...)

这意味着type指的是,这与我们从内部保存this.type的值相同。typecontextcall

于 2012-08-01T10:51:57.923 回答
1

是的,您可以通过定义success()inside来解决这个问题call,所以这具有相同的范围:

function call(name){
    var type = services[name].type;
    function success(data) {
                fbposts=data.data
                    if (type === "grupp"){
                        var postid=fbposts[fbpost].id.split("_");
                        return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/'
                    }
                    else if (fbposts[fbpost].actions){
                        return fbposts[fbpost].actions[0].link;
                    }
                }
            }       
        })

    $.oajax({
        success: success   
    })
};

替代解决方案是将其存储在外部,例如。在某个外部范围内,在某个变量中,或附加到 DOM 元素之一。

您还可以使用包装器,以正确的类型调用将返回知道这种类型的成功函数,如下所示:

function make_success(type){
    return function(data){
        // do what you need for success callback
    };
}
var my_success_callback = make_success('some_type');
于 2012-08-01T10:50:39.233 回答
0

您可以让成功函数返回您需要传递给 ajax 的函数,并将类型变量传递给父函数——就像 Python 装饰器一样。这是一个例子:

function call(name){
    var type = services[name].type;

    $.ajax({
        success: success(type)
    })
};

function success(type) {
    return function(data) {
        // We have access to 'type' in here!

        fbposts=data.data;
        if (type === "grupp"){
            var postid=fbposts[fbpost].id.split("_");
            return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/'
        }
        else if (fbposts[fbpost].actions){
            return fbposts[fbpost].actions[0].link;
        }
    }
}
于 2012-08-01T10:54:13.807 回答