19장 프로토타입(2)

이재민·2023년 7월 26일
0

자바스크립트

목록 보기
16/17

프로토타입의 생성 시점

프로토타입은 생성자 함수가 생성되는 시점에 더불어 생성됨.

사용자 정의 생성자 함수와 프로토타입 생성 시점

생성자 함수로서 호출할 수 있는 함수, 즉 constructor는 함수 정의가 평가되어 함수 객체를 생성하는 시점에 프로토타입도 더불어 생성됨.

// 함수 정의(constructor)가 평가되어 함수 객체를 생성하는 시점에 프로토타입도 더불어 생성
console.log(Person.prototype); // {constructor: f}

// 생성자 함수
function Person(name){
    this.name = name;
}

non-constructor는 프로토타입이 생성되지 않음.

// 화살표 함수는 non-constructor
const Person = name => {
    this.name = name;
};
// non-constructor는 프로토타입이 생성되지 않음.
console.log(Person.prototype); // undefined

함수 선언문은 런타임 이전에 자바스크립트엔진에 의해 먼저 실행.

이처럼 빌트인 생성자 함수가 아닌 사용자 정의 생성자 함수는 자신이 평가되어 함수 객체로 생성되는 시점에 프로토타입도 더불어 생성되며, 생성된 프로토타입의 프로토타입은 언제자 Object.prototype임.

빌트인 생성자 함수와 프로토타입 생성 시점

모든 빌트인 생성자 함수는 전역객체가 생성되는 시점에 생성.

객체가 생성되기 이전에 생성자 함수와 프로토타입은 이미 객체화 되어 존재. 이후 생성자 함수 또는 리터럴 표기법으로 객체를 생성하면 프로토타입은 생성된 객체의 [[Prototype]] 내부 슬롯에 할당됨.

객체 생성 방식과 프로토타입 결정

객체 생성에는 다양한 방식이 있음

  • 객체 리터럴
  • Object 생성자 함수
  • 생성자 함수
  • Object.create 메서드
  • 클래스(ES6)

각 방식에는 세부적인 차이가 있으나 추상 연산 OrdinaryObjectCreate에 의해 생성된다는 공통점 있음

객체 리터럴에 의해 생성된 객체의 프로토타입

const obj = {};

obj객체는 Object.prototype을 프로토타입으로 갖게 되며, Object.prototype을 상속 받음. obj객체는 constructor 프로퍼티와 hasOwnProperty 메서드를 소유하지 않지만 자신의 프로토타입인 Object.prototype의 것을 자신의 자산인 것처럼 사용함.

obj객체가 자신의 프로토타입인 Object.prototype 객체를 상속 받기때문.

const obj = {x:1};

// 객체 리터럴에 의해 생성된 obj 객체는 Object.prototype을 상속받음.
console.log(obj.constructor === Object); // true
console.log(obj.hasOwnProperty('x')); // true

Object 생성자 함수에 의해 생성된 객체의 프로토타입

const obj = new Object();
obj.x = 1;

OrdinaryObjectCreate에 의해 Object 생성자 함수와 Object.prototype과 생성된 객체 사이에 연결이 만들어짐.

obj객체는 Object.prototype을 프로토타입으로 갖게 되며, Object.prototype을 상속 받음.

const obj = new Object();
obj.x = 1;

console.log(obj.constructor === Object); // true
console.log(obj.hasOwnProperty('x')); // true

생성자 함수에 의해 생성된 객체의 프로토타입

function Person(name){
    this.name = name;
}
const me = new Person('Lee');

사용자 저의 생성자 함수 Person 더불어 생성된 프로토타입 Person.prototype의 프로토타입은 constructor뿐임.

프로토타입은 객체임. 따라서 일반 객체와 같이 프로토타입에도 프로퍼티를 추가/삭제 가능. 이는 프로토타입 체인에 즉각 반영.

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

// 프로토타입 메서드
Person.prototype.sayHello = function(){
    console.log(`Hi My name is ${this.name}`);
}
const me = new Person('Lee');
const you = new Person('Kim');

me.sayHello() // Hi My name is Lee
you.sayHello() // Hi My name is Kim

프로토타입 체인

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

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

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

// 프로토타입 메서드
Person.prototype.sayHello = function(){
    console.log(`Hi My name is ${this.name}`);
}
const me = new Person('Lee');
console.log(me.hasOwnProperty('name')); // true

me 객체는 Person.prototype뿐만 아니라 Object.prototype을 상속받음.

me 객체의 프로토타입은 Person.Prototype임.

프로토타입의 프로토타입은 언제나 Object.prototype

모든 객체는 Object.prototype을 상속받음. Object.prototype을 프로토타입 체인의 종점이라 함.

프로토타입 체인은 상속과 프로퍼티 검색을 위한 매커니즘

스코프 체인은 식별자 검색을 위한 매커니즘

me.hasOwnProperty('name');
// 스코프 체인에서 me 식별자 검색 -> me 식별자는 전역에서 선언되었으므로 전역 스코프에서 검색. me 식별자를 검색한 다음, me 객체의 프로토타입 체인에서 hasOwnProperty메서드를 검색. 

위의 예시처럼, 스코프 체인과 프로토타입 체인은 서로 협력하여 식별자와 프로퍼티를 검색하는 데 사용.

오버라이딩과 프로토타입 섀도잉

const Person = (function(){
   // 생성자 함수
   function Person(name){
   	this.name = name;
	}
   // 프로토타입 메서드
   Person.prototype.sayHello = function(){
       console.log(`Hi! My name is ${this.name}`);
   };
   
   // 생성자 함수
   return Person;
}());
const me = new Person('Lee');

// 인스턴스 메서드
me.sayHello = function(){
   console.log(`Hi! My name is ${this.name}`);
};

// 인스턴스 메서드가 호출됨. 프로토타입 메서드는 인스턴스 메서드에 의해 가려짐.

프로토타입 프로퍼티와 같은 이름의 프로퍼티를 인스턴스에 추가하면 프로토타입 프로퍼티를 덮어쓰는 것이 아니라 인스턴스 프로퍼티로 추가. 상속 관계에 의해 프로퍼티가 가려지는 현상을 프로퍼티 섀도잉이라 함.

// 인스턴스 메서드를 삭제함
delete me.sayHello;
// 인스턴스에는 sayHello 메서드가 없으므로 프로토타입 메서드가 호출됨.
me.sayHello(); // Hi! My name is is Lee;

// 프로토타입 체인을 통해 프로토타입 메서드가 삭제되지 않음.
delete me.sayHello;

// 하위 객체를 통해 프로토타입의 프로퍼티를 변경 또는 삭제 불가능

프로토타입 교체

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

const me = new Person('Lee');
// 프로토타입을 교체하면 constructor 프로퍼티와 생성자 함수 간의 연결이 파괴.
console.log(me.constructor === Person); // false
console.log(me.constructor === Object); // true

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

인스턴스의 __prototype__ 접근자 프로퍼티를 통해 프로토타입 교체.

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

const me = new Person('Lee');

// 프로토타입으로 교체할 객체
const parent(){
    sayHello(){
        console.log(`Hi! My name is ${this.name}`);
    }
};

// me 객체의 프로토타입을 parent 객체로 교체.
Object.setPrototypeOf(me, parent);
// 위의 코드는 아래의 코드와 동일하게 동작.
// me.__prototpye__ = parent;
me.sayHello(); // Hi! My name is Lee
// 프로토타입을 교체하면 constructor 프로퍼티와 생성자 함수 간의 연결이 파괴.
console.log(me.constructor === Person); // false
console.log(me.constructor === Object); // true  

instanceof 연산자

객체 instanceof 생성자 함수

우변의 생성자 함수의 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); // false
// 생성자 함수
function Person(name){
    this.name = name;
}
const me = new Person('Lee');

const parent = {};
// 프로토타입 교체
Object.setPrototypeOf(me, parent);

// Person 생성자 함수와 parent 객체는 연결되어 있지 않음
console.log(me instanceof Person); // false;
console.log(me instanceof Object); // false;

// Person.prototype이 me 객체의 프로토타입 체인 상에 존재하지 않기 때문에 false
console.log(me instanceof Person); // false
// Object.prototype이 me 객체의 프로토타입 체인 상에 존재하므로 true
console.log(me instanceof Object); // true

// parent 객체를 Person 생성자 함수의 prototype 프로퍼티에 바인딩
Person.prototype = parent;
// Person.prototype이 me 객체의 프로토타입 체인 상에 존재하므로 true
console.log(me instanceof Person); // true
// Object.prototype이 me 객체의 프로토타입 체인상에 존재하므로 true
console.log(me instanceof Object); // true

instanceof 연산자는 생성자 함수의 prototype애 바인딩된 객체가 프로토타입 체인 상에 존재하는지 확인.

직접 상속

Object.create에 의한 직접 상속

첫 번째 매개변수에는 생성할 객체의 프로토타입으로 지정할 객체 전달.

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

두 번째 인수는 옵션이므로 생략 가능

// 프로토타입이 null인 객체를 생성. 생성된 객체는 프로토타입 체인의 종점에 위치
// obj -> null
let obj = Object.create(null);
console.log(Object.getPrototype(obj) == null); // true
// Object.prototype을 상속받지 못함.
console.log(obj.toString()); // TypeError

// obj -> Object.prototype -> null
// obj = {};와 동일
obj = Object.create(Object.prototype);
console.log(Object.getPrototypeOf(obj) === Object.prototype); // true

// obj -> Object.prototype -> null
// obj = { x:1 };와 동일
obj = Object.create(Object.prototpye),{
    x: {Value: 1, writable: true, enumerable: true, configurable: true}
});
// 위 코드는 아래와 동일.
// obj = Object.create(Object.prototype);
// obj.x = 1;
console.log(obj.x); // 1
console.log(Object.prototypeOf(obj) == Object.prototype); // true

const myProto = {x:10};
// 임의의 객체를 직접 상속
// obj -> myProto -> Object.prototype -> null
obj = Object.create(myProto);
console.log(obj.x);
console.log(Object.getPrototypeOf(obj)=== myProto); // true

// 생성자 함수
function Person(name){
    this.name = name;
}
// obj -> Person.prototype -> object.prototype -> null
// obj = new Person('Lee')와 동일
obj = Object.create(Person.prototype);
obj.name = 'Lee';
console.log(obj.name); // Lee
console.log(Object.getPrototypeOf(obj) === Person.prototype); // true;

Object.create 메서드는 객체를 생성하면서 직접적으로 상속을 구현하는 것임.

장점으로

  • new 연산자 없이도 객체 생성 가능
  • 프로토타입을 지정하면서 객체를 생성
  • 객체 리터럴에 의해 생성된 객체도 상속받을 수 있음

참고로 프로토타입 체인의 종점에 위치하는 객체는 Object.prototype의 빌트인 메서드를 사용할 수 없음

// 프로토타입 체인의 종점에 위치하는 객체를 생성
const obj = Object.create(null);
obj.a = 1;
console.log(Object.getPrototypeOf(obj)==null); // true

// obj는 Object.prototype의 빌트인 메서드를 사용할 수 없음.
console.log(obj.hasOwnProperty('a')) // TypeError

//에러가 발생하기에 간접 호출하는 것이 좋음.
console.log(Object.prototype.hasOwnProperty.call(obj, 'a')) // true

객체 리터럴 내부에서 __prototype__ 에 의한 직접 상속

ES6에서는 객체 리터럴 내부에서 __prototype__ 접근자 프로퍼티를 사용하여 직접 상속을 구현.

const myProto = {x:10};
const obj = {
    y:20,
    // 객체를 직접 상속
    // obj -> myProto -> Object.prototype ->null
    __proto__:myProto
};
/*위의 코드는 아래와 동일
const obj = Object.create(myProto,{
	y: {value:20, writable: true, enumerable: true, configurable: true}
});
*/
console.log(obj.x, obj.y); // 10 20
console.log(Object.getPrototypeOf(obj)===myProto);; // true

정적 프로퍼티/메서드

// 생성자 함수
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(){
    sonsole.log('staticMethod');
}
const me = new Person('Lee');

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

// 정적 프로퍼티/메서드는 생성자 함수가 생성한 인스턴스로 참조/호출 불가
// 인스턴스로 참조/호출할 수 있는 프로퍼티/ 메서드는 프로토타입 체인 상에 존재해야 함.
me.staticMethod(); // TypeError : me.staticMethod is not a function
// Object.create는 Object 생성자 함수정적 메서드
const obj = Object.create({name: 'Lee'});

// Object.prototype.hasOwnProperty는 프로토타입 메서드
obj.hasOwnProperty('name'); // false

프로퍼티 존재 확인

in 연산자는 객체 내에 특정 프로퍼티가 존재하는지 여부를 확인.

const person = {
    name : 'Lee',
    address: 'Seoul'
};
// person 객체에 name 프로퍼티가 존재.
console.log('name' in person); // true
console.log('age' in person); // false

console.log('toString' in person); // true

in 연산자는 확인 대상 객체의 프로퍼티뿐 아니라 확인 대상 객체가 상속 받은 모든 프로토타입의 프로퍼티를 확인하므로 주의.

in 연산자 대신 ES6에 도입된 Reflect.has 메서드를 사용할 수도 있음. Reflect.has 메서드는 in 연산자와 동일하게 동작.

const person = {name: 'Lee'}

console.log(Reflect.has(person, 'name')); // true
console.log(Reflect.has(person, 'toString')); // true

Object.prototype.hasOwnProperty 메서드

특정 프러퍼티가 존재하는지 확인 가능.

const person = {name: 'Lee'}
console.log(person.hasOwnProperty('name')); // true
console.log(person.hasOwnProperty('age')); // false
// 상속 받은 프로토타입의 프로퍼티 키인 경우 false를 반환.
console.log(person.hasOwnProperty('toString')); // false

프로퍼티 열거

객체의 모든 프로퍼티를 순회하며 열거하려면 for...in 문을 사용.

객체의 프로토타입 체인 상에 존재하는 모든 프로토타입의 프로퍼티 중에서 프로퍼티 어트리뷰트 [[Enumerable]]의 값이 true인 프로퍼티를 순회하며 열거.

for (변수선어문 in 객체){
   ...
}
   
const person = {
    name : 'Lee',
    address : 'Seoul'
};
// for...in 문의 변수 key에 person 객체의 프로퍼티 키가 할당.
for(const key in person){
    console.log(key + ': ' + person[key]);
}
// name : Lee
// address : Seoul

for...in 문은 in 연산자처럼 순회 대상 객체의 프로퍼티뿐만 아니라 상속박은 프로토타입의 프로퍼티까지 열거.

키가 심벌인 프로퍼티는 열거하지 않음.

하지만 toString과 같은 Object.prototype의 프로토타입은 열거 x

이유는 열거할 수 없도록 정의되어 있는 프로퍼티이기 때문임. [[Enumerable]]의 값이 false

// Object.getOwnPropertyDescriptor 메서드는 프로퍼티 디스크립터 객체를 반환
// 프로퍼티 디스크립터 객체는 프로퍼티 어트리뷰트 정보를 담고 있는 객체
console.log(Object.getOwnPropertyDescriptor(Object.prototype, 'toString'));
// {value: f, writable: true, enumerable: false, configurable: true}

객체 자신의 프로퍼티만 열거하려면 Object.prototype.hasProperty 메서드를 사용하여 객체 자신의 프로퍼티인지 확인.

const person = {
    name : 'Lee',
    address: 'Seoul',
    __proto__ : {age : 20}
};

for (const key in person){
    // 객체 자신의 프로퍼티인지 확인
    if(!person.hasOwnProperty(key)) continue;
    console.log(key + ': ' + person[key]);
}
// name : Lee
// address : Seoul

for... in 문은 vmfhvjxlfmf 열거할 때 순서를 보장하지 않으므로 주의가 필요.

하지만 대부분의 모던 브라우저는 순서를 보장, 숫자인 프로퍼티 키에 대해서는 정렬을 실시.

배열에는 for...in문을 서용하지 말고 일반적인 for문이나 for...of문 또는 forEach메서드를 권장.

const arr = [1,2,3];
arr.x =10; // 배열도 객체이므로 프로퍼티를 가질 수 있음.

for(const i in arr){
    // 프로퍼티 x도 출력
    // 1 2 3 10
};

// arr.length는 3
for(let i = 0; i<arr.length; i++){
    console.log(arr[i])
    // 1 2 3
}

// forEach 메서드는 요소가 아닌 프로퍼티는 제외
arr.Each(v=>console.log(v)); // 1 2 3

// for...of는 변수 선언문에서 선언한 변수에 키가 아닌 값을 할당.
for(const value of arr){
    // 1 2 3
}

Object.keys/values/entries 메서드

객체 자신의 열거 가능한 프로퍼티 키를 배열로 반환.

const person = {
    name : 'Lee',
    address : 'Seoul',
    __prototype__ : {age : 20}
    
};
console.log(Object.keys(person)); // ["name", "address"]

ES8에서 도입된 Object.values 메서드는 객체 자신의 열거 가능한 프로퍼티 값을 배열로 반환.

console.log(Object.values(person)) // ["Lee", "Seoul"]

ES8에서 도입된 Object.entries 메서드는 객체 자신의 열거 가능한 프로퍼티 키와 값의 쌍의 배열을 배열에 담아 반환.

console.log(Object.entries(person)); // [["name", "Lee"], ["address", "Seoul"]]
Object.entries(person).forEach([key, value]) => console.log(key, value));
/*
name Lee
address Seoul
*/
profile
안녕하세요

1개의 댓글

comment-user-thumbnail
2023년 7월 26일

정보 감사합니다.

답글 달기