I have this code that fails when I try to use the for loop
class TestA {
testArray = [];
constructor() {
this.testArray.push({
name: "joe",
age: 70
});
this.testArray.push({
name: "mike",
age: 50
});
this.testArray.push({
name: "bob",
age: 33
});
}
testLoop() {
for (test of this.testArray) {
console.log(" >>> " + test.name + " " + test.age);
}
}
}
var a = new TestA();
a.testLoop();
The error:
Uncaught ReferenceError: test is not defined
I can get away with that by using different kind of loop but don't understand why that doesn't work?
for (const test of this.testArray)test oftolet test of?for (let test of ....).push(). Just add all the objects as arguments to a single call, and they all get added to the array.this.testArray.push({...}, {...}, {...})classdefinition, you're running in strict mode, which sets more stringent requirements. Outsideclassyou can manually put your code into that mode by using the"use strict"declarative.