Arrays of Objects in OOP in JavaScript
Class objects can be stored in an array and various operations can be performed on them, just like on array elements. Let's look at an example. Let's say we have the following class:
javascriptclass User {
#name;
constructor(name) {
this.#name = name;
}
getName() {
return this.#name;
}
}
Let's make an array of objects of this class:
javascriptlet users = [
new User('john'),
new User('eric'),
new User('kyle'),
];
Let's loop through this array and print out the name of each user:
javascriptfor (let user of users) {
console.log(user.getName());
}
Задание 1
Make an array of class objects Employee.
Задание 2
Loop through the created array and display the names and salaries of each employee.