I am trying to create an array collection of objects inside a JavaScript class.
class Person {
object = {};
employees = new Array();
constructor() {}
addPerson(name, age) {
this.object.name = name;
this.object.age = age;
this.employees.push(this.object);
}
getGroupInfo() {
return this.employees;
}
}
let p1 = new Person();
p1.addPerson("Johnny", 40);
p1.addPerson("Mark", 55);
console.log(p1.getGroupInfo());
The output seems to override with the second input that I am providing.
[
{ name: 'Mark', age: 55 },
{ name: 'Mark', age: 55 }
]
I am certainly failing to understand the basic javascript way of class creation :-)