프로토타입 체인

이창훈·2022년 7월 3일
0

프로토타입 체인

function Person(name){
	this.name;
}

Person.prototype.sayHello = function(){
	console.log(`Hi My name is ${this.name}`)
}

const me = new Person('Lee')

console.log(me.hasOwnProperty('name')); //true
  • Person 생성자 함수에 의해 생성된 me 객체는 Object.prototype의 메서드인 hasOwnProperty를 호출할 수 있으므로 me 객체는 Person.prototype 뿐 아니라 Object.prototype도 상속받았다는 것을 의미한다.

  • Person.prototype의 프로토타입은 Object.prototype이다.

  • 모든 객체는 Object.prototype을 상속받는다.

  • 프로토타입의 프로토타입은 언제나 Object.prototype(end of prototype chain)이다.

  • 따라서 Object.prototype의 프로토타입, 즉 [[Prototype]] 내부 슬롯의 값은 null이다.

  • Object.prototype에서도 프로퍼티를 검색할 수 없는 경우 undefined를 반환한다. 에러가 발생하지 않는다!

  • 자바스크립트는 객체의 프로퍼티(메서드 포함)에 접근하려고 할 때 해당 객체에 접근하려는 프로퍼티가 없다면 [[Prototype]] 내부 슬롯의 참조를 따라 자신의 부모역할을 하는 프로토타입의 프로퍼티를 순차적으로 검색하다.

  • 이를 프로토타입 체인이라 한다.

  • 프로토타입 체인은 자바스크립트가 객체지향 프로그래밍의 상속을 구현하는 메커니즘이다.

me.hasOwnProperty('name')의 검색 과정

  1. 먼저 hasOwnProperty 메서드를 호출한 me 객체에서 hasOwnProperty 메서드를 검색한다.

  2. me 객체에는 hasOwnProperty 메서드가 없으므로 프로토타입 체인을 따라, 다시 말해 [[Prototype]] 내부 슬롯에 바인딩되어 있는 프로토타입으로 이동하여 hasOwnProperty 메서드를 검색한다.

  3. Person.prototype에도 hasOwnProperty 메서드가 없으므로 프로토타입 체인을 따라, 다시 말해 [[Prototype]] 내부 슬롯에 바인딩되어 있는 프로토타입으로 이동하여 hasOwnProperty 메서드를 검색한다.

  4. Object.prototype에는 hasOwnProperty 메서드가 존재한다. 자바스크립트 엔진은 Object.prototype.hasOwnProperty 메서드를 호출한다. 이때 Object.prototype.hasOwnProperty 메서드의 this에는 me 객체가 바인딩된다.

Object.prototype.hasOwnProperty.call(me, 'name')
  • call 메서드는 this로 사용할 객체를 전달하면서 함수를 호출한다. this로 사용할 me 객체를 전달하면서 Object.prototype.hasOwnProperty 메서드를 호출한다.

19.8 오버라이딩과 프로퍼티 섀도잉

19.9 프로토타입의 교체

  • 프로토타입은 임의의 다른 객체로 변경할 수 있다.

19.9.2 인스턴스에 의한 프로토타입 교체

  • 프로토타입은 생성자 함수의 prototype 프로퍼티뿐아니라 인스턴스의 __proto__ 접근자 프로퍼티(또는 Object.setPrototypeOf 메서드)를 통해 프로토타입을 교체할 수 있다.

  • 생성자 함수의 prototype 프로퍼티에 다른 임의의 객체를 바인딩하는 것은 미래에 생성할 인스턴스의 프로토타입을 교체하는 것이다.

  • __proto__ 접근자 프로퍼티를 통해 프로토타입을 교체하는 것은 이미 생성된 객체의 프로토타입을 교체하는 것이다.

function Person(name) {
	this.name = name;
}

const me = new Person('Lee')

const parent = {
	sayHello(){
    	console.log('Hi! My name is ${this.name}')
    }
}

Object.setPrototypeOf(me, parent);

me.sayHello(); Hi My name is Lee
  • prototype으로 교체한 객체에는 constructor 프로퍼티가 없으므로 constructor 프로퍼티와 생성자 함수 간의 연결이 파괴된다.

  • 따라서 프로토타입의 constructor 프로퍼티로 me 객체의 생성자 함수를 검색하면 Person이 아닌 Object가 나온다.

console.log(me.constructor === Person) ; //false
console.log(me.constructor === Object); //true
  • 프로토타입으로 교체한 객체 리터럴에 constructor 프로퍼티를 추가하고 생성자 함수의 prototype 프로퍼티를 재설정하여 파괴된 생성자 함수와 프로토타입 간의 연결을 되살려 보자.
function Peron(name){
	this.name = name;
}

const me = new Person('Lee');

// 프로토타입으로 교체할 객체
const parent  = {
	//constructor 프로퍼티와 생성자 함수 간의 연결을 설정
    constructor : Person,
    sayHello(){
    	console.log(`Hi! My name is ${this.name}`)
    }
};

// 생성자 함수의 prototype 프로퍼티와 프로토타입 간의 연결을 설정
Person.prototype = parent;

// me 객체의 프로토타입을 parent 객체로 교체한다.
Object.setPrototypeOf(me, parent);
// 위 코드는 아래의 코드와 동일함
// me.__proto__ = parent;

me.sayHello(); // Hi! My name is Lee

console.log(me.constructor === Person) // true
console.log(me.constructor === Object) // false

console.log(Person.prototype === Object.getPrototypeOf(me)); //true

instanceof 연산자

  • instanceof 연산자는 이항 연산자로서 좌변에 객체를 가리키는 식별자, 우변에 생성자 함수를 가리키는 식별자를 피연산자로 받는다.
  • 만약 우변의 피연산자가 함수가 아닌 경우 TypeError가 발생한다.

  • 우변의 생성자 함수의 prototype에 바인딩된 객체가 좌변의 객체의 프로토타입 체인 상에 존재하면 true로 평가되고, 그렇지 않으면 false로 평가된다.

function Person(name){
	this.name = name;
}

const me = new Person('Lee');
console.log(me instanceof Person); //true
console.log(me instanceof Object); //true

//프로토타입으로 교체할 객체
const parent = {}

//프로토타입의 교체
Object.setPrototypeof(me, parent);

console.log(me instanceof Person) //false
console.log(me instanceof Object) //true
  • me 객체는 비록 프로토타입이 교체되어 프로토타입과 생성자 함수 간의 연결이 파괴되었지만 Person 생성자 함수에 의해 생성된 인스턴스이다.

  • 그러나 me instanceof Person은 false로 평가된다.

  • 이는 Person.prototype이 me 객체의 프로토타입 체인 상에 존재하지 않기 때문이다.

  • instanceof 연산자는 프로토타입의 constructor 프로퍼티가 가리키는 생성자 함수를 찾는것이 아니라 생성자 함수의 prototype에 바인딩된 객체가 프로토타입 체인 상에 존재하는지 확인한다.

19.11 직접상속

19.11.1 Object.create에 의한 직접 상속

  • Object.create 메서드는 명시적으로 프로토타입을 지정하여 새로운 객체를 생성한다.

  • Object.create 메서드의 첫 번째 매개변수에는 생성할 객체의 프로토타입으로 지정할 객체를 전달한다.

  • 두 번째 매개변수에는 생성할 객체의 프로퍼티 키와 프로퍼티 디스크럽터 객체로 이뤄진 객체를 전달한다.

  • 두 번째 인수는 옵션이므로 생략가능하다.

  • Object.create 메서드는 첫 번째 매개변수에 전달한 객체의 프로토타입 체인에 속하는 객체를 생성한다.

  • 즉, 객체를 생성하면서 직접적으로 상속을 구현하는 것이다.

  • Object.create 메서드의 장점

  1. new 연산자가 없이도 객체를 생성할 수 있다.
  2. 프로토타입을 지정하면서 객체를 생성할 수 있따.
  3. 객체 리터럴에 의해 생성된 객체도 상속받을 수 있다.
  • 따라서 Object.prototype 빌트인 메서드는 다음과 같이 간접적으로 호출하는 것이 좋다.

19.12 정적 프로퍼티/메서드

  • 정적static 프로퍼티/메서드는 생성자 함수로 인스턴스를 생성하지 않아도 참조/호출할 수 있는 프로퍼티/메서드를 말한다.
//생성자 함수
function Person(name){
	this.name = name
} 

//프로토타입 메서드
Person.prototype.sayHello = function(){
	console.log(`Hi! My name is ${this.name}`);
}

//정적 프로퍼티
Person.staticProp = 'static prop';

//정적 메서드
Person.staticMethod = function(){
	console.log('staticMethod');
};


const me = new Person('Lee')

// 생성자 함수에 추가한 정적 프로퍼티/메서드는 생성자 함수로 참조/호출한다.
Person.staticMethod();

// 정적 프로퍼티/메서드는 생성자 함수가 생성한 인스턴스로 참조/호출할 수 없다.
// 인스턴스로 참조/호출할 수 있는 프로퍼티/메서드는 프로토타입 체인 상에 존재해야 한다.
me.staticMethod(); //typeError
  • Person 생성자 함수는 객체이므로 자신의 프로퍼티/메소드를 소유할 수 있다.

19.13 프로퍼티 존재 확인

19.13.1 in 연산자.

  • in 연산자는 객체 내에 특정 프로퍼티가 존재하는지 여부를 확인한다.
const person = {
	name : "Lee",
    address : "Seoul"
}

console.log( 'name' in person) // true
console.log( 'toString' in person) // true
  • in 연산자는 확인 대상 객체의 프로퍼티뿐만 아니라 확인 대상 객체가 상속받은 모든 프로토타입의 프로퍼티를 확인한다.

  • toString 프로퍼티는 Object.prototype의 메서드이다.

  • in 연산자 대신 ES6에서 도입된 Reflect.has 메서드를 사용할 수도 있다.

  • Reflect.has 메서드는 in 연산자와 동일하게 작동한다.

19.13.2 Object.prototype.hasOwnProperty 메서드

  • Object.prototype.hasOwnProperty 메서드를 사용하면 객체에 특정 프로퍼티가 있는지 확일 할 수 있다.

  • Object.prototype.hasOwnProperty 메서드는 이름에서 알 수 있듯이 인수로 전달받은 프로퍼티 키가 객체 고유의 프로퍼티 키인 경우에만 true를 반환하고 상속받은 프로토타입의 프로퍼티 키인 경우 false를 반환한다.

const person = {
	name : "Lee",
    address : "Seoul"
}

console.log(person.prototype.hasOwnProperty('name')) // true
console.log(person.prototype.hasOwnProperty('age')) // false

19.14 프로퍼티 열거

for ... in 문

  • 객체의 모든 프로퍼티를 순회하며 열거하려면 for ... in 문을 사용한다.
profile
실패를 두려워하지 않고 배우고 기록하여 내일의 밑거름 삼아 다음 단계로 성장하겠습니다.

0개의 댓글