fffo

클래스 상속 본문

Programming/Javascript

클래스 상속

gggs 2021. 10. 15. 22:24

상속에 의한 클래스 확장

  • 프로토타입 기반 상속은 프로토타입 체인을 통해 다른 객체의 자산을 상속받는 개념인 반면 상속에 의한 클래스 확장은 기존 클래스를 상속받아 새로운 클래스를 확장하여 정의하는 것
class Animal {
  constructor(age, weight) {
    this.age = age;
    this.weight = weight;
  }

  eat() {
    return 'eat';
  }

  move() {
    return 'move';
  }
}

class Bird extends Animal {
  fly() {
    return 'fly';
  }
}

const bird = new Bird(1, 5);

console.log(bird); // Bird { age: 1, weight: 5 }
console.log(bird instanceof Animal); // true
console.log(bird instanceof Bird); // true
console.log(bird.eat()); // eat
console.log(bird.move()); // move
console.log(bird.fly()); // fly
// 위 클래스를 흉내낸 생성자 함수
const Animal = (function () {
  function Animal(age, weight) {
    this.age = age;
    this.weight = weight;
  }
  Animal.prototype.eat = function () { return 'eat'};
  Animal.prototype.move = function () { return 'move'};
  return Animal
})();

const Bird = (function () {
  function Bird() {
    Animal.apply(this, arguments); // (1)
  }
  Bird.prototype = Object.create(Animal.prototype); // (2)
  Bird.prototype.constructor = Bird; // (3)
  Bird.prototype.fly = function () { return 'fly'};
  return Bird;
})();

(1) Animal 생성자 함수를 this바인딩 없이 호출 시 일반 함수로 취급되어 this에 전역 객체를 넣고 Animal 생성자 함수 호출 됨. new를 통해 생성자 함수로 호출해도 Animal 생성자 함수가 생성할 인스턴스가 this에 바인딩 되기 때문에 Bird의 this는 빈객체가 됨. 따라서 Bird의 this를 Animal 생성자 함수의 this에 바인딩 하여 실행시킴

(2) Animal.prototype을 프로토타입으로 갖는 객체로 Bird.prototype을 교체

(3) Bird.prototype.constructor를 Animal에서 Bird로 교체

extends 키워드

// 수퍼(베이스/부모) 클래스
class Base {}

// 서브(파생/자식) 클래스
class Derived extends Base {}
  • 수퍼클래스와 서브클래스는 인스턴스의 프로토타입 체인 뿐만 아니라 클래스간의 프로토타입 체인도 생성함. → 정적 메서드도 상속됨

동적 상속

  • extends 키워드는 [[Construct]] 내부 메서드를 갖는 함수 객체로 평가될 수 있는 모든 표현식을 상속 받을 수 있음
  • 그러나 extends 키워드 앞에는 반드시 클래스가 와야함
function Base1() {}
class Base2 {}
let condition = true;

// 조건에 따라 동적으로 상속 대상을 결정하는 서브클래스
class Derived extends (condition ? Base1 : Base2) {}

서브클래스의 constructor

  • 서브클래스에서 constructor 생략 시 다음과 같은 constructor가 암묵적으로 정의됨
// args는 클래스 호출 시 전달한 인수의 리스트
constructor(...args) { super(...args); }
  • super는 수퍼클래스의 constructor를 호출해 인스턴스를 생성함

super 키워드

  • super 키워드는 함수처럼 호출할 수도 있고 식별자처럼 참조도 할 수 있음
    • 호출 시 : 수퍼클래스의 constructor 호출
    • 참조 시 : 수퍼클래스의 메서드 호출할 수 있음

super 호출

class Base {
  constructor (a, b) {
    this.a = a;
    this.b = b;
  }
}

class Derived extends Base {
  constructor (a, b ,c) {
    super(a,b);
    this.c = c;
  }
}

const derived = new Derived(1,2,3);
console.log(derived); // Derived { a: 1, b: 2, c: 3 }
  • 서브클래스에서 constructor를 생략하지 않으면 constructor내에 반드시 super를 호출해야함
  • 서브클래스의 constructor에서 super를 호출하기 전에는 this를 참조할 수 없음
  • super는 반드시 서브클래스의 constructor에서만 호출해야 함

super 참조

  • 서브클래스의 프로토타입 메서드 내에서 super를 참조할 시 수퍼클래스의 프로토타입 메서드를 호출할 수 있음
  • 서브클래스의 정적 메서드 내에서 super를 참조할 시 수퍼클래스의 정적메서드를 호출할 수 있음
class Base {
  constructor(name) {
    this.name = name;
  }

  sayHi() {
    return `Hi ${this.name}!`;
  }
}

class Derived extends Base {
  sayHi() {
    return `${super.sayHi()} How is it going?`;
  }
}

const derived = new Derived('dlwlrma');
console.log(derived.sayHi()); // Hi dlwlrma! How is it going?
  • super는 자신을 참조하고 있는 메서드가 바인딩되어있는 객체의 프로토타입을 가리킴
    • sayHi가 바인딩 되어있는 객체 Base
    • Base의 프로토타입 Base.prototype
    • super 는 Base.prototype
  • super 참조가 동작하기 위해서는 super를 참조하고 있는 메서드가 바인딩되어있는 객체의 프로토타입을 찾을 수 있어야함
    • 이를 위해 메서드는 내부슬롯 [[HomeObject]]를 가지며 자신을 바인딩하고 있는 객체를 가리킴
    • ES6의 메서드 축약 표현으로 정의된 함수만이 [[HomeObject]]를 가짐
  • 메서드 축약 표현으로 정의되었다면 객체 리터럴에서도 super참조 가능
const base = {
  name : 'dlwlrma',
    sayHi() {
        return `Hi ${this.name}!`;
  }
}

const derived = {
  __proto__ : base,
  sayHi() {
    return `${super.sayHi()} How is it going?`;
  }
}

console.log(derived.sayHi()); // Hi dlwlrma! How is it going?

상속클래스의 인스턴스 생성 과정

1. 서브클래스의 super 호출

  • js엔진은 클래스 평가 시 부자관계를 구분하기 위해 내부슬롯 [[ConstructorKind]]를 가지고 여기에 "base"나 "derived"를 값으로 가짐
  • 서브클래스는 [[ConstructorKind]]에 derived 값을 가져서 호출 시 부모클래스와 구분되는 동작을 가짐
  • 서브클래스는 자신이 직접 인스턴스를 생성하지 않고 수퍼클래스에 인스턴스 생성을 위임 → 서브클래스의 constructor에서 super를 반드시 호출해야하는 이유

2. 수퍼클래스의 인스턴스 생성과 this바인딩

  • 수퍼클래스에 constructor 내부 코드 실행되기 이전에 빈 객체 생성, this에해당 빈 객체를 바인딩
  • 이 때 new 연산자와 함께 호출된 클래스는 서브 클래스이므로 new.target은 서브클래스를 가리킴 → 인스턴스는 new.target이 가리키는 서브클래스가 생성한 것으로 처리됨
  • 생성된 인스턴스의 프로토타입은 서브클래스의 prototype 프로퍼티가 가리키는 객체임

3. 수퍼클래스의 인스턴스 초기화

  • 수퍼클래스의 constructor 실행되어 인스턴스 초기화

4. 서브클래스 constructor로의 복귀와 this바인딩

  • super의 호출 종료, 제어 흐름이 서브클래스 constructor로 돌아옴
  • 서브클래스는 별도로 인스턴스를 생성하지 않고 super가 반환한 인스턴스를 this에 바인딩 해 그대로 사용함 → constructor에서 super를 호출하기 전 this를 참조할 수 없는 이유

5. 서브클래스의 인스턴스 초기화

  • 서브클래스의 constructor 실행, 인스턴스 초기화

6. 인스턴스 반환

  • 완성된 인스턴스가 바인딩된 this를 암묵적으로 반환

표준 빌트인 생성자 함수 확장

  • 표준 빌트인 객체(String, Number, Array 등)도 [[Construct]] 내부 메서드를 갖는 생성자 함수이므로 extends 키워드를 사용해 확장 가능

'Programming > Javascript' 카테고리의 다른 글

배열  (0) 2021.10.19
ES6 함수의 추가 기능  (0) 2021.10.16
클래스  (0) 2021.10.13
클로저  (0) 2021.10.11
실행 컨텍스트  (0) 2021.10.07
Comments