0

私はいくつかのクラス (Hello) を持っています。このクラスには foo プロパティがあり、このプロパティは xhr リクエストの後に入力する必要があります。から設定fooするXMLHttpRequest方法と呼び出す方法はafterLoad()

function Hello(){

    this.foo = null;

    this.process = function(){
        var req = new XMLHttpRequest();
        req.open('GET', 'http://some.url', true);
        req.onload = function(){
            // How to set Hello.foo in this context?
            // And how to call Hello.afterLoad() from this context?
            // this == XMLHttpRequest instance
        };
        req.send(null);
    }
    this.afterLoad = function(){
        console.log(this.foo);
        // Some stuff goes here
    }
}
4

1 に答える 1

1
function Hello(){

  this.foo = null;

  this.process = function(){
    var _that = this,
        req = new XMLHttpRequest();
    req.open('GET', 'http://some.url', true);
    req.onload = function(){
        // How to set Hello.foo in this context?
        // And how to call Hello.afterLoad() from this context?
        // this == XMLHttpRequest instance
        _that.foo = 'something';
        _that.afterLoad();
    };
    req.send(null);
  }
  this.afterLoad = function(){
    console.log(this.foo);
    // Some stuff goes here
  }
}
于 2013-01-23T13:50:39.110 に答える