【发布时间】:2018-08-23 13:24:18
【问题描述】:
构造函数“this.A”创建实例,然后发送到构造函数“this.b”。一个数组由两个构造函数实例编译而成。无法从存储它们的数组中访问来自“this.b”实例的值。收到“无法获取未定义的属性 'calc'”错误。但是,可以在填充数组后在最后访问值。这已经过时了,因为 'var data' 是无穷无尽的值流,因此数组会不断被填充。
var data = [-1,1,4,3,-3,-4,1,-4];
function init() {
this.A = function(value, time) {
this.value = value;
this.time = time
}
this.B = function(point, array) {
this.calc = function() {
var x = point.value;
if(x>=0){
return 'positive'
} else {
return 'negative'
}
}
}
this.time = 0;
this.arrayA = [];
this.arrayB = [];
}
function update (datastream) {
var time = this.time += 1;
var value = datastream[time];
var x = new this.A(value, time);
var y = new this.B(x, this.arrayA);
this.arrayA.push(x);
this.arrayB.push(y);
//error:
console.log(this.arrayB[time].calc())
}
function finish() {
for(let x=0; x < data.length; x++) {
console.log(this.arrayB[x].calc())
}
}
init();
for(let x=0; x < data.length; x++) {
update(data)
};
finish()
【问题讨论】:
标签: javascript constructor undefined