Error: "cannot read property 'name'"


#1


i'm stuck here: https://www.codecademy.com/en/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661#


When i run my code i get : "TypeError: Cannot read property 'name' of undefined"


var Person = function(name, age) {
    this.name = name;
    this.age = age;
}

// Now we can make an array of people
var family = new Array();
family[0] = Person("alice", 40);
family[1] = Person("bob", 42);
family[2] = Person("michelle", 8);
family[3] = Person("timmy", 6);

// loop through our new array
for (var i = 0; i < family.length; i++) {
    console.log(family[i].name);
}


#2

family[0] = Person("alice", 40);

You forgot the keyword "new", to instantiate a new Person object (similar to how you instantiated your family Array).

family[0] = new Person("alice", 40);

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.