I'm trying to create a object using my constructor Person, but it doesn't work when I initialize the object directly in the array that uses literal notation.
function Person (name, age) {
this.name = name;
this.age = age;
}
var family = [
[ new Person("alice", 40) ],
[ new Person("bob", 42) ],
[ new Person("michelle", 8) ],
[ new Person("timmy", 6) ]
];
for (var person in family) {
console.log(family[person].name);
}
But it just prints undefined four times.
I gotta use this notation:
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);
So it prints alice, bob, michelle, timmy.
What am I doing wrong?
forloop, you can just as easily throw it into aforEach