instanceof operatorius OOP JavaScripte

Operatorius instanceof leidžia patikrinti ar objektas priklauso tam tikrai klasei. Pažiūrėkime pavyzdžiu. Tarkime, kad turime šią klasę:

javascriptclass User {
  
}

Sukurkime objektą iš šios klasės:

javascriptlet user = new User;

Patikrinkime, ar objektas iš kintamojo priklauso mūsų klasei:

javascriptconsole.log(user instanceof User); // true
Задание 1

Nustatykite, koks bus rezultatas vykdant šį kodą:

javascriptclass Student {
  
}
class Employee {
  
}

let employee = new Employee;
console.log(employee instanceof Employee);
console.log(employee instanceof Student);
Задание 2

Duotas toks kodas:

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'),
];

Peržiūrėkite ciklu objektų masyvą ir išvestį į konsolę tik darbuotojų vardus.