이를 위해 instanceof
연산자를 사용할 수 있습니다 . MDN에서 :
instanceof 연산자는 생성자의 프로토 타입 속성이 객체의 프로토 타입 체인에 나타나는지 테스트합니다.
어떤 프로토 타입과 프로토 타입 체인을 모르는 경우 찾아 보는 것이 좋습니다. 또한 개념을 명확히 할 수있는 JS (TS는 이와 관련하여 유사하게 작동합니다) 예제가 있습니다.
class Animal {
name;
constructor(name) {
this.name = name;
}
}
const animal = new Animal('fluffy');
// true because Animal in on the prototype chain of animal
console.log(animal instanceof Animal); // true
// Proof that Animal is on the prototype chain
console.log(Object.getPrototypeOf(animal) === Animal.prototype); // true
// true because Object in on the prototype chain of animal
console.log(animal instanceof Object);
// Proof that Object is on the prototype chain
console.log(Object.getPrototypeOf(Animal.prototype) === Object.prototype); // true
console.log(animal instanceof Function); // false, Function not on prototype chain
이 예제의 프로토 타입 체인은 다음과 같습니다.
동물> Animal.prototype> Object.prototype
Std.is(pet, Fish)
유형, 인터페이스 등에서 작동 하는 신뢰할 수있는 유형 검사 기능이있는 Haxe를 사용하게되어 기쁩니다 .