Аператар instanceof у ААП у JavaScript
Аператар instanceof дазваляе праверыць
належыць аб'ект пэўнаму класу.
Давайце паглядзім на прыкладзе. Хай у нас ёсць
наступны клас:
class User {
}
Зробім аб'ект з гэтага класу:
let user = new User;
Праверым, належыць аб'ект з зменнай нашаму класу:
console.log(user instanceof User); // true
Вызначыце, якім будзе вынік выканання наступнага кода:
class Student {
}
class Employee {
}
let employee = new Employee;
console.log(employee instanceof Employee);
console.log(employee instanceof Student);
Даны наступны код:
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'),
];
Перабярыце цыклам масіў аб'ектаў і вывядзіце ў кансоль толькі імёны працоўнікаў.