The instanceof Operator in OOP in JavaScript
The instanceof operator allows you to check whether an object belongs to a certain class. Let's look at an example. Let's say we have the following class:
class User {
}
Let's make an object of this class:
let user = new User;
Let's check if the object from the variable belongs to our class:
console.log(user instanceof User); // true
Determine what the result of executing the following code will be:
class Student {
}
class Employee {
}
let employee = new Employee;
console.log(employee instanceof Employee);
console.log(employee instanceof Student);
The following code is given:
class Student {
constructor(name) {
this.name = name;
}
}
class Employee {
constructor(name) {
this.name = name;
}
}
let users = [
new Student('user1'),
new Employee('user2'),
new Student('user3'),
new Employee('user4'),
new Student('user5'),
new Employee('user6'),
new Student('user7'),
];
Loop through the array of objects and output only the names of the employees to the console.