2010-05-09 60 views
0
MyObj = 
{ 
    ajax: null, 
    init: function() 
    { 
     this.ajax = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] 
      .createInstance(); 
     this.ajax.onload = function() 
     { 
      return function() {this.onload.apply(this, [null]);} 
     }; 
    }, 
    onload: function() 
    { 
     Reader.log("I really hope I can use `this.ajax` here"); 
    } 
} 

是不是正确的方式来绑定onload到MyObj?出于某种原因onload从未被调用。但是,如果我避免绑定,只是把this.ajax.onload = this.onload然后onload调用。如何获得约束力的工作?javascript绑定

回答

1

,我会回答自己...

我应该叫功能

var me = this; 

this.ajax.onload = (function() 
     { 
      return function() {me.onload.apply(me, [null]);} 
     })(); 
+1

为什么额外的功能封装在那里? this.ajax.onload = function(){me.onload.apply(me,[null]);} – Joshua 2010-05-09 03:15:52

+0

@ Joshua:你是对的,那是不必要的,它甚至没有它。 – Pablo 2010-05-09 03:18:05