Javascript对象的属性没有设置

时间:2014-03-11 22:21:11

标签: javascript parsing csv properties

有一点javascript问题: 我有一组单独的.js文件,一个充当“类”,这是我的解析器 - “类”,它基本上存储CSV文件的内容及其随机部分(csv-file是“country;资本“),解析工作正常:

var allLocations;
var rndLocations;

function CSVparser() {
this.allLocations = new Array;
this.rndLocations = new Array;

//parses the CSV into an Array ("Country;City")
this.parse = function(parser) {
    $.ajax({
        type : "GET",
        url : "ressource/capitalCountryList.txt",
        dataType : "text",
        success : function(data) {
            parser.allLocations = data.split(/\r\n|\n/);
            var min = 0;
            var max = parser.allLocations.length - 1;
            for (var i = 1; i <= 9; i++) {
                var rndIndex = (Math.floor(Math.random() * (max - min + 1) + min));
                parser.rndLocations.push(parser.allLocations[rndIndex]);
            }
        }
    });
  };
}

在我的主类中,我现在启动一个解析器,并调用parse() - function:

var parser = new CSVparser();
parser.parse(parser);

如果我现在在我的主类(console.log(parser))中记录解析器,我能够看到解析器属性的内容(例如rndLocations数组)。但是如果我想通过

访问我的主类里面的这个数组
parser.rndLocations;

数组为空。

那里的问题是什么?对不起,我的英语不太完美!感谢帮助!

0 个答案:

没有答案