JQuery:在回调中引用外部范围 [英] JQuery: Referencing outer scope within callback

查看:117
本文介绍了JQuery:在回调中引用外部范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与OO Javascript和一个jQuery回调的问题。
如果你看下面的例子,它应该解释一切。

I have an issue with OO Javascript and a jQuery callback. If you look at the sample below it should explain everything.

如何调用functionToCall()深入这个函数。

How do I call functionToCall() deep within this functception.

function outerClass() {
    this.functionToCall = function() {
        //do something
    }

    this.someOtherFunction = function() {

    this.aCoupleOfVariables1 = 2;
    this.aCoupleOfVariables2 = "stuff";

    $.ajax({
        success: function() {
        //How do I call functionToCall() right here
        //TRIED:
            functionToCall();
            this.functionToCall();
            that.functionToCall();
        }
    }); 
    }
}


推荐答案

可以将作为上下文选项传递给 $。ajax()

$.ajax({
    context: this,
    success: function() {
        // Here, 'this' refers to the same object as in the caller.
        this.functionToCall();  
    }
});

这篇关于JQuery:在回调中引用外部范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆