[javascript] 객체의 프로토타입을 확인하는 방법

자바스크립트에서 객체의 프로토타입을 확인하는 방법은 여러 가지가 있습니다. 여기에는 Object.getPrototypeOf() 메서드, instanceof 연산자, isPrototypeOf() 메서드 등이 있습니다.

Object.getPrototypeOf() 메서드

const obj = {};
const prototype = Object.getPrototypeOf(obj);
console.log(prototype); // Object {}

Object.getPrototypeOf() 메서드는 주어진 객체의 프로토타입을 반환합니다.

instanceof 연산자

function Foo() {}
const foo = new Foo();
console.log(foo instanceof Foo); // true
console.log(foo instanceof Object); // true

instanceof 연산자는 주어진 객체가 특정 클래스의 인스턴스인지 여부를 확인합니다.

isPrototypeOf() 메서드

function Bar() {}
const bar = new Bar();
console.log(Bar.prototype.isPrototypeOf(bar)); // true

isPrototypeOf() 메서드는 특정 프로토타입이 주어진 객체의 프로토타입 체인 내에 있는지 확인합니다.

Object.getPrototypeOf() 메서드를 사용하여 객체의 프로토타입을 확인하는 방법이 가장 일반적입니다.

결론

객체의 프로토타입을 확인하는 방법으로 Object.getPrototypeOf() 메서드를 사용할 수 있습니다. 또한, instanceof 연산자와 isPrototypeOf() 메서드도 유용한 방법입니다. 프로토타입을 확인하여 객체의 상속 관계를 명확히 이해할 수 있습니다.

참고문헌: