Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
instanceof 연산자는 생성자의 prototype 속성이 객체의 프로토타입 체인 어딘가 존재하는지 판별합니다.
판별할 객체.
constructor판별 목표 함수.
instanceof 연산자는 object의 프로토타입 체인에 constructor.prototype이 존재하는지 판별합니다.
instanceof 의 값은 생성자 prototype 프로퍼티의 변화에 따라 바뀔수 있으며, Object.setPrototypeOf의 사용함에 따라서도 바뀔 수 있음에 주의하세요. 또한 non-standard의 __proto__ 슈도-프로퍼티도 사용할 수 있도록 만들어 줍니다.
다른 스코프는 다른 실행 환경을 가집니다. 이것은 다른 스코프는 다른 고정된 요소들(다른 전역 오브젝트, 다른 생성자들 등)을 가지고 있음을 의미합니다. 이 사실은 예상치 못한 결과를 가져올 수도 있습니다. 예를 들면, [] instanceof window.frames[0].Array는 false를 리턴할 것입니다. 왜냐하면, Array.prototype !== window.frames[0].Array 이며, arrays 는 상위로부터 상속받기 때문입니다. 이것은 처음에는 말이 되지 않을 수도 있습니다. 하지만, 스크립트에서 여러 프레임이나 창을 다루며, 객체를 함수를 통하여 하나의 컨텍스트에서 다른 컨텍스트로 오브젝트를 넘기게 된다면, 이건 충분히 일어날 수 있는 일이며, 아주 큰 이슈가 될 것입니다. 예를 들어, 주어진 오브젝트가 실제로 Array.isArray(myObj)를 사용한 Array인지 안전하게 확인 할 수 있습니다.
참고 : XPCOM instanceof 을 사용하는 코드에서는 특별한 효과를 가집니다. : obj instanceof xpcomInterface (예. Components.interfaces.nsIFile)가 obj.QueryInterface(xpcomInterface)를 호출하고, 만약 QueryInterface 가 성공하면 true 를 리턴합니다. 이 호출의 부가 효과로는 obj 에서 성공적인 instanceof 테스트 후에 xpcomInterface's의 프로퍼티를 사용할 수 있습니다. 스탠다드 JavaScript 전역속성들과는 달리, 테스트 obj instanceof xpcomInterface는 obj가 다른 스코프에 있더라도 작동합니다.
아래의 코드는 instanceof 를 String 과 Date 오브젝트도 타입 Object임을 확인하기 위해 사용합니다.(이 오브젝트들은 Object 로부터 파생되었습니다.)
그러나, 여기서 오브젝트 리터럴 노테이션으로 생성된 오브젝트는 예외적입니다. : 비록 프로토 타입이 정의되지 않았지만, instanceof Object 는 true를 리턴합니다.
다음의 코드는 Car 오브젝트 타입과 그 오브젝트 타입의 인스턴스 mycar 를 생성합니다. instanceof 연산자는 mycar 오브젝트는 타입 Car 와 타입 Object 라는 것은 보여줍니다.
| ECMAScript® 2027 Language Specification # sec-relational-operators |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025년 2월 11일 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.