음악, 삶, 개발
instanceof 키워드 본문
< 참고 강좌 : youtu.be/_DLhUBWsRtw?t=1279 >
instanceof 를 통해, 특정 인스턴스가 해당 클래스의 인스턴스인지 boolean 으로 확인해주는 방법이다.
class Shape {}
class Rectangle extends Shape {}
class Triangle extends Shape {}
const r = new Rectangle
const t = new Triangle
console.log(r instanceof Rectangle) // true
console.log(t instanceof Triangle) // true
console.log(r instanceof Shape) // true
console.log(t instanceof Shape) // true
console.log(r instanceof Object) // true
console.log(r instanceof Triangle) // false
console.log(t instanceof Rectangle) // false
여기서 중요한것은 자식의 인스턴스는 부모 클래스의 인스턴스 이기도 하다는것.
하지만 형제간에는 성립되지않는다.
또한, 모든 인스턴스는 Object 의 인스턴스이다.