假设我有一个功能:
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' }
编辑:
其实method01就像这样:
test.prototype.method02=function(){ $('.cpy').resizable({ }).draggable({ start:function(e,ui){ this.method01(); } }); }
解决方法
test.prototype.method02=function(){ var testThing = this; $('.cpy').resizable({ }).draggable({ start:function(e,ui){ testThing.method01(); } }); }
您必须在另一个本地变量中保留此引用,以便在调用其他方法时回调函数可以使用它.这个引用被绑定在每个函数调用上,包括对回调函数的调用,如你在“.draggable()”设置中使用的函数.当这被称为这将被设置为与“method02”功能不同的东西.