JavaScript面向对象参考方法

JavaScript面向对象参考方法,javascript,jquery,Javascript,Jquery,早上好,我正在从JavaScript的函数式编程方法转向面向对象方法,我有一个问题。在函数式编程中,我可以调用另一个函数示例中的函数: function a(){ // do something and when done call function b b(); } function b(){ // does more stuff } 现在我切换到OOP方法,如何从同一对象中的另一个方法调用对象中的方法。例如: var myClass = function(){

早上好,我正在从JavaScript的函数式编程方法转向面向对象方法,我有一个问题。在函数式编程中,我可以调用另一个函数示例中的函数:

function a(){
    // do something and when done call function b
    b();
}

function b(){ 
    // does more stuff 
}
现在我切换到OOP方法,如何从同一对象中的另一个方法调用对象中的方法。例如:

var myClass = function(){
    this.getData = function(){
        //do a jquery load and on success call the next method
        $('#a').load('file.asp',function(response,status,xhr){
            switch(status){
                case "success":
                    //THIS IS WHERE THE QUESTION LIES
                    this.otherfuntcion();
                break;
            }
        }
    }

    this.otherfunction = new function(){
        // does more stuff
    }
}

p = new myClass();
p.getData();
在成功调用方法b时我可以这样说吗?还是我必须做其他事情?提前感谢。

匿名回调函数中的此上下文与类方法中的上下文不同。因此,您需要在闭包中保留对上下文的引用:

var that = this;
$('#a').load('file.asp',function(response,status,xhr){
    switch(status){
        case "success":
            //THIS IS WHERE THE QUESTION LIES
            that.otherfuntcion();
        break;
    }
});
另一种方法是将特定上下文绑定到匿名函数:

$('#a').load('file.asp',function(response,status,xhr){
    switch(status){
        case "success":
            //THIS IS WHERE THE QUESTION LIES
            this.otherfuntcion();
        break;
    }
}.bind(this));
匿名回调函数中的this上下文与类的方法中的上下文不同。因此,您需要在闭包中保留对上下文的引用:

var that = this;
$('#a').load('file.asp',function(response,status,xhr){
    switch(status){
        case "success":
            //THIS IS WHERE THE QUESTION LIES
            that.otherfuntcion();
        break;
    }
});
另一种方法是将特定上下文绑定到匿名函数:

$('#a').load('file.asp',function(response,status,xhr){
    switch(status){
        case "success":
            //THIS IS WHERE THE QUESTION LIES
            this.otherfuntcion();
        break;
    }
}.bind(this));

如果使用更多的方法和大量的实例,这将非常缓慢。改用原型:

var myClass = function(){

}
myClass.prototype = {
    getData: function(){
        //do a jquery load and on success call the next method
        $('#a').load('file.asp',function(response,status,xhr){
            switch(status){
                case "success":
                    //THIS IS WHERE THE QUESTION LIES
                    this.otherfunction();
                break;
            }
        }.bind(this))
    },
    otherfunction: new function(){
        // does more stuff
    }
};


p = new myClass();
p.getData();

如果使用更多的方法和大量的实例,这将非常缓慢。改用原型:

var myClass = function(){

}
myClass.prototype = {
    getData: function(){
        //do a jquery load and on success call the next method
        $('#a').load('file.asp',function(response,status,xhr){
            switch(status){
                case "success":
                    //THIS IS WHERE THE QUESTION LIES
                    this.otherfunction();
                break;
            }
        }.bind(this))
    },
    otherfunction: new function(){
        // does more stuff
    }
};


p = new myClass();
p.getData();

您应该将外部函数上下文复制到新变量中,以直接引用外部上下文。这在内部函数中是这个内部函数的上下文

var self = this;
$('#a').load('file.asp',function(response,status,xhr){
    switch(status){
        case "success":
            //THIS IS WHERE THE QUESTION LIES
            self.otherfuntcion();
        break;
    }
}

您应该将外部函数上下文复制到新变量中,以直接引用外部上下文。这在内部函数中是这个内部函数的上下文

var self = this;
$('#a').load('file.asp',function(response,status,xhr){
    switch(status){
        case "success":
            //THIS IS WHERE THE QUESTION LIES
            self.otherfuntcion();
        break;
    }
}

拼写错误。这个.others也可以作为一个资源来帮助您。。拼写错误。这个.others也可以作为一个资源来帮助您。。这不是指jqXHR对象的内部完整回调吗?@inf3rno,恕我冒昧,但我对这种编程方式还不熟悉。为什么原型会更快?因为它是一个核心js功能,所以它是在您的js引擎中实现的,例如在v8中由chrome实现。它会更快,因为您的类的所有实例将共享相同的内存。@inf3rno,我假设我可以在主myClass函数中声明可以在所有原型中引用的变量?我可以直接在原型中用var名称来调用它们,对吗?这不是指jqXHR对象的内部完整回调吗?@inf3rno,恕我直言,但我对这种编程方式还不熟悉。为什么原型会更快?因为它是一个核心js功能,所以它是在您的js引擎中实现的,例如在v8中由chrome实现。它会更快,因为您的类的所有实例将共享相同的内存。@inf3rno,我假设我可以在主myClass函数中声明可以在所有原型中引用的变量?我可以在原型中用var名称来调用它们,对吗?在这些情况下,我们通常调用bind来更改范围,使用它、self等。。。在很长一段时间内不建议使用。在这些情况下,我们通常调用绑定来更改范围,使用that、self等。。。在很长一段时间内不建议使用。