JavaScript-dä OOP-da instance operator
instanceof operator bir objektiň belli bir klasa degişlidigini barlamaga mümkinçilik berýär.
Mysala garalyň. Bizde aşakdaky klas bolsun:
class User {
}
Bu klasdan bir objekt döredeliň:
let user = new User;
Değişjandaky objektiň bizim klasymyza degişlidigini barlaýalyň:
console.log(user instanceof User); // true
Aşakdaky kodyň ýerine ýetiriliş netijesiniň näme boljakdygyny kesgitleň:
class Student {
}
class Employee {
}
let employee = new Employee;
console.log(employee instanceof Employee);
console.log(employee instanceof Student);
Aşakdaky kod berlen:
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'),
];
Objektleriň massiwini aýlaw bilen geziň we diňe işgärleriň adlaryny konsola çap ediň.