26. Array of Objects


#1


hey guys,
so i perfectly understand how this works but i have a question.
what if i dont want to use the new keyword, and i just want to make an array like i would normally e.g
var family = [];

how do i then use the person constructor to create objects?


// Our person constructor
function Person (name, age) {
    this.name = name;
    this.age = age;
}

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


#2

You still need to use new to create new Person objects:

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)
];

#3

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