如何通过另一种方法填充的方法访问对象的成员? [英] how to access object's member through a method which filled by another method?

查看:46
本文介绍了如何通过另一种方法填充的方法访问对象的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道标题有点混乱,这里有详细信息:

I know the title is a little bit confusion, here is the details:

假设我在javascript中定义了一个自定义对象,并且定义了一个公共成员在其中:

Say I have a custom object defined in javascript, and there is a public member defined in it:

 function Test()
 {
   this.testArray = [];
 }

我有两个方法用于此对象,一个是读出一些xml文件并填入数组:

And I have two methods for this object, one is read out some xml file and filled into the array:

 Test.prototype.readXML = function()
 {
   var self = this;
   $.get('assest/xml/mydata.xml', function(d){
    $(d).find("item").each(function(){
            var item = new Item;
            item.ID = ($(this).attr("ID"));
            item.body = ($(this).find("body").text());
            });
     self.testArray.push(item);
   });
   }

另一个函数,它将内容显示在HTML页面中。

And another function, which will display the content into the HTML page.

  Test.prototype.appendInfo = function()
  {
     var i;
     for (i=0; i<testArray.length;i++)
     {
       $('#testdisplay').append(testArray[i].ID +"<br />");
       $('#testdisplay').append(testArray[i].body = "<br /");
      }
   }

然而,显示功能继续给我错误testArray未定义。我不确定问题出在哪里,因为我将显示功能放在阅读功能之后。我希望数据存储在数组中,并且可以在我需要的时候访问。

However, the display function continue gives me error that the testArray is not defined. I'm not sure where is the problem, since I put the display function behind the reading function. I expect that the data will be stored in the array and could be accessed anytime I need them.

希望有人能帮助我解决这个问题!谢谢!
}
}

Hope some one will kindly help me about this! Thank you! } }

推荐答案

所以我发现你的代码存在两个问题。

So I notice two problems with your code.

首先,当你进行ajax调用时,你需要将延迟传递给用户。由于ajax调用是异步的,因此它可能无法立即完成。

First when you do your ajax call you need to pass a deferred back to the user. Since ajax calls are async it may not finish right away.

所以你的readXML函数应该这样做。它应该返回jquery get。

So your readXML function should do this. It should return the jquery get.

Test.prototype.readXML = function() {
   var self = this;
   return $.get('assest/xml/mydata.xml', function(d){
        $(d).find("item").each(function(){
            var item = new Item;
            item.ID = ($(this).attr("ID"));
            item.body = ($(this).find("body").text());
        });
        self.testArray.push(item);
   });
}

接下来你的第二个函数追加只是缺少一些上下文。

Next you your second function append was just missing some context.

Test.prototype.appendInfo = function() {
    var i;
    for (i=0; i<this.testArray.length;i++) {
       $('#testdisplay').append(this.testArray[i].ID +"<br />");
       $('#testdisplay').append(this.testArray[i].body = "<br /");
    }
}

所以你的代码应该是这样的。

So your code should look like this.

var mytest = new Test();

mytest.readXML().done(function(){
    mytest.appendInfo();
}).fail(function(){
    // put some fallback code here
});

更新:
添加了额外的这个。

Updated: Added additional this's.

这篇关于如何通过另一种方法填充的方法访问对象的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆