开发者

javascript binding

开发者 https://www.devze.com 2022-12-29 10:45 出处:网络
MyObj = { ajax: null, init: function() { this.ajax = Cc[\"@mozilla.org/xmlextras/xmlhttpreque开发者_如何学编程st;1\"]
MyObj = 
{
    ajax: null,
    init: function()
    {
        this.ajax = Cc["@mozilla.org/xmlextras/xmlhttpreque开发者_如何学编程st;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");
    }
}

isn't it correct way to bind onload to MyObj? For some reason onload never called. However if I avoid binding and just put this.ajax.onload = this.onload then onload invoked. How to get binding work?


and I will answer myself...

I should have called the function

var me = this;

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

精彩评论

暂无评论...
验证码 换一张
取 消