new 연산자로 인스턴스 생성
class 표현식
let name = class {}
let name = class inner_name{}
let name = class extends super_name{}
let name = class inner_name extends super_name{}
strict 모드에서 실행
클래스가 function과 비슷하지만 특징있음
“use strict” 를 작성하지 않아도 됨
즉, ES6는 strict 모드가 기본환경
주요 strict 모드 제약
eval(), with() 사용불가
var을 사용하지 않고 변수 선언 불가
클래스에 메서드 작성 방법
getName 형태로 작성
메서드 이름만 작성
function 과 콜론을 작성하지 않음
메서드 사이에 콤마를 사용하지 않음
메스드 끝에 세미콜론 작성은 선택
글로벌 오브젝트에 설정되지 않음
window.class_Name: undefined 반환
constructor 사용
new 연산자가 constructor 호출
엔진은 우선 Object{}를 생성하고
인스턴스에 필요한 프로퍼티를 Object에 설정
생성한 Object가 인스턴스
constructor 블록 코드 싱행
this로 2번에서 생성한 인스턴스 참조
- 인스턴스를 먼저 생성하므로 this로 참조 가능
class Memeber {
constructor(name) {
this.name = name
};
getName() {
return this.name;
}
}
getter
메서드 앞에 getter 작성
class Memeber {
get getName() {
return this.name;
}
}
상속
클래스를 상속받음
extends
subClass extends superClass()
ES6는 extends 키워드로 상속 구현
class Soccer extends Sports()