ตัวสร้าง 'this.A' สร้างอินสแตนซ์ที่ถูกส่งไปยังตัวสร้าง 'this.b' อาร์เรย์ถูกคอมไพล์จากอินสแตนซ์คอนสตรัคเตอร์ทั้งสอง ไม่สามารถเข้าถึงค่าจากอินสแตนซ์ 'this.b' จากอาร์เรย์ที่เก็บไว้ ได้รับข้อผิดพลาด 'ไม่สามารถรับคุณสมบัติ 'คำนวณ' ของไม่ได้กำหนด' อย่างไรก็ตามคุณสามารถเข้าถึงค่าได้ในตอนท้ายหลังจากกรอกอาร์เรย์แล้ว สิ่งนี้ล้าสมัยเนื่องจาก '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()