class Person {}
로 선언const Person = class {}
constructor()
이 런타임 이전에 평가되어 프로토타입을 생성한다.__proto__
: class.prototypeprototype
: constructor__proto__
: Function.prototypeconstructor()
은 보이지 않는다.constructor
constructor(name = "person")
처럼 기본값 부여 가능기본적인 메서드 = 프로토타입 메서드
sayHi()
=== 생성자 함수의 Person.prototype.sayHi = function()
me -> Person.prototype의 -> Object.prototype
Person.prototype의 constructor : Person class
static sayHi()
=== 생성자 함수의 Person.sayHi()
[[Enumerable]]
=== false
[[Construct]]
존재 XJAVA, C# 처럼 클래스 몸체에 필드(변수)를 사용할 수 있도록 하는 것
몸체에서 정의가능
내부에서 참조시 반드시 this를 사용
정의시 프로토타입이 아니라 인스턴스에 할당되어 권장 X
sayHi()
: 프로토타입에 할당sayHi = function() {...}
: 인스턴스에 할당 (클래스 필드)#age
static age = 22;
프로토타입 기반의 상속과 상속에 의한 클래스 확장은 다르다.
prototype
을 상속받는다.수퍼클래스의 constructor()
를 호출
constructor(...args) {super(...args)}
가 정의된다.constructor(a,b,c) {super(a,b); this.c = c;}
처럼 수퍼클래스와 서브 클래스가 협력하여 인스턴스를 생성할 수도 있다.constructor()
을 생략하지 않을 경우 그 안에 반드시 super()
가 있어야 한다.super()
호출 전에는 this를 참조할 수 없다.super()
은 반드시 서브클래스에서만 호출해야한다.수퍼클래스의 메서드를 호출
super.sayHi()
수퍼클래스와 서브 클래스의 협력을 통한 인스턴스 생성과정
서브클래스에서의 super 호출
(!상속받음 ? "base" : "derived")
수퍼클래스의 인스턴스 생성과 this 바인딩
수퍼클래스의 인스턴스 초기화
서브클래스 constructor로의 복귀와 this바인딩
서브클래스의 인스턴스 초기화
인스턴스 반환