JavaScript OOP의 instanceof 연산자

연산자 instanceof는 객체가 특정 클래스에 속하는지 확인할 수 있게 해줍니다. 예제를 통해 살펴보겠습니다. 다음과 같은 클래스가 있다고 가정해 봅시다:

javascriptclass User {
  
}

이 클래스의 객체를 만들어 보겠습니다:

javascriptlet user = new User;

변수에서 가져온 객체가 우리 클래스에 속하는지 확인해 보겠습니다:

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

다음 코드를 실행한 결과가 무엇일지 결정하세요:

javascriptclass Student {
  
}
class Employee {
  
}

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

다음 코드가 주어졌습니다:

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

객체 배열을 순환하며 콘솔에 직원의 이름만 출력하세요.