无法将值从实例对象传输到父类的实例数组中

Unable to transfer values from instance object into instance array of parent class

本文关键字:实例 父类 数组 传输 对象      更新时间:2024-03-16

我有一个JS类,在里面我正在实例化一个goog.net.XhrIo对象。现在我可以从JS变量中获得我需要的所有东西,问题是每当我试图为"父"类的实例变量赋值时,我都会得到一个未定义的错误。我认为这是一个范围界定问题,但我不知道如何处理它。我正在使用谷歌闭包库。代码可在以下位置找到:

主JS文件

我正在尝试将POST请求的所有结果存储在this.items对象/数组中。行号是270。如果你在浏览器中看到这个,搜索"console.log(this.mainView);",它会直接带你找到问题所在。

goog.net.XhrIo.send(this.URL+'/action.url.php', function(e){
    var items = new Object();
    items = e.target.getResponseJson();
    for(var x in items)
    {
        var currentName;
        var currentCat;
        if(items.hasOwnProperty(x))
        {
            currentName = items[x].name;
            currentCat = items[x].categories;
        }
        var x = goog.dom.createDom('div', {
        }, currentName, currentCat);
        console.log(this.mainView);
    }
    return e;
},'POST', 'action=getUserData');

尝试使用goog.bind:将处理程序绑定到对象

goog.net.XhrIo.send(this.URL+'/action.url.php', goog.bind( function(e){
    var items = new Object();
    items = e.target.getResponseJson();
    for(var x in items)
    {
        var currentName;
        var currentCat;
        if(items.hasOwnProperty(x))
        {
            currentName = items[x].name;
            currentCat = items[x].categories;
        }
        var x = goog.dom.createDom('div', {
        }, currentName, currentCat);
        console.log(this.mainView);
    }
    return e;
}, this),'POST', 'action=getUserData');