Not sure what the title should be really, but I was working on a project but came across this error, so I made a simple representation of it: When i instantiate a new object (The student), the student has a certain number of classes, AFTER initializing this object I then add another class to the list of classes, which then is passed to a new student object. When I output their classes the console says they have the same number of classes, I just do not understand why "student1" seems to update their list AFTER it was started, can somebody help? (init is ran when HTML Body is loaded)
function init() {
let newList = [];
newList.push(new Class("Maths"));
newList.push(new Class("English"));
let student1 = new Student("Bob", "1", "2", newList);
let newList2 = newList;
newList2.push(new Class("Science"));
let student2 = new Student("Bob", "2", "1", newList2);
console.log(student1.list);
console.log(student2.list);
}
class Student {
constructor(firstName, lastName, age, classes) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.list = classes;
}
}
class Class {
constructor(subject) {
this.subject = subject;
}
}