在javascript中如何在另一个原型方法中调用一个原型方法?

hh5*_*188 10 javascript prototype call

假设我有一个功能:

function test(){}

test.prototype.method01=function(){
    //do something
}

test.prototype.method02=function(){
    //how can I call the method01?
    //this.method01()...?
    //but the chrome through an error:
    //Uncaught TypeError: Object #<HTMLImageElement> has no method 'method01'
}
Run Code Online (Sandbox Code Playgroud)

编辑:实际上方法01是这样的:

test.prototype.method02=function(){
    $('.cpy').resizable({

    }).draggable({
        start:function(e,ui){
            this.method01();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

Poi*_*nty 13

test.prototype.method02=function(){
    var testThing = this;
    $('.cpy').resizable({

    }).draggable({
        start:function(e,ui){
            testThing.method01();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

您必须this在另一个局部变量中保留引用,以便回调函数在调用另一个方法时可以使用它.将this在每一个函数调用引用绑定,包括调用回调像你正在使用的".draggable()"设置的一个功能.当被调用的this内容设置为与this"method02"函数不同时.