我有以下代码块用于类声明:
class Node {
constructor(alias, statement, additionalInformation, source) {
this.id;
this.alias = alias;
this.statement = statement;
this.additionalInformation = additionalInformation;
this.source = source;
this.getInfo = getNodeInfo;
}
}
我有这段代码来填充节点对象数组:
var arrNodes = [];
var i = 0;
$.getJSON('source.json', function(data){
$(data.nodes).each(function (index,value) {
arrNodes.push(new Node(value.alias, value.statement,value.additionalInformation, value.source));
console.log(arrNodes[i].alias, arrNodes[i].statement, arrNodes[i].source);
i++;
});
});
直到这一刻一切正常,我能够获得我从控制台记录的那些值。但每次我试图分别访问任何数组元素时,让我们这样说:
console.log(arrNodes[0].alias);
我得到以下信息:
Uncaught TypeError: Cannot read property 'alias' of undefined
你能帮我找到解决这个问题的方法吗?