JavaScript-da OOP-da instanceof operatori
instanceof operatori obyektning ma'lum bir classga tegishli ekanligini tekshirish imkonini beradi.
Keling, bir misolni ko'rib chiqaylik. Faraz qilaylik, bizda quyidagi class mavjud:
javascriptclass User {
}
Ushbu classdan obyekt yaratamiz:
javascriptlet user = new User;
O'zgaruvchidagi obyekt bizning classimizga tegishli ekanligini teksiramiz:
javascriptconsole.log(user instanceof User); // true
Задание 1
Quyidagi kodni bajarish natijasi qanday bo'lishini aniqlang:
javascriptclass Student {
}
class Employee {
}
let employee = new Employee;
console.log(employee instanceof Employee);
console.log(employee instanceof Student);
Задание 2
Quyidagi kod berilgan:
javascriptclass 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'),
];
Obyektlar massivini tsikl yordamida aylantiring va faqat xodimlarning ismlarini konsolga chiqaring.