class 선언(declaration)은 프로토타입(원형) 기반 상속을 사용하여 주어진 이름으로 새로운 클래스를 만듭니다.
class 식(expression)을 사용하여 클래스를 정의할 수도 있습니다.
구문
class name [extends] {
// class body
}
설명
class 식에서처럼, class 선언의 class 본문(body)은 엄격 모드에서 실행됩니다.
class 선언은 (끌어올려)지지 않습니다 (function 선언과는 달리).
예
간단한 클래스 선언
다음 예에서, 먼저 Polygon 클래스를 정의합니다. 그 뒤에 Square 클래스를 만들기 위해 그것을 확장합니다. 생성자(constructor)에 사용된 super()는 생성자에서만 쓰일 수 있고 this 키워드가 사용되기 전에 호출되어야 함을 주의하세요.
class Polygon {
constructor(height, width) {
this.name = 'Polygon';
this.height = height;
this.width = width;
}
}
class Square extends Polygon {
constructor(length) {
super(length, length);
this.name = 'Square';
}
}
스펙
브라우저 호환성
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 42.0 | 45 (45) | ? | ? | ? |
| Array subclassing | 43.0 | No support | ? | ? | ? |
| Allowed in sloppy mode | 49.0 |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | No support | 42.0 | 45.0 (45) | ? | ? | ? | 42.0 |
| Array subclassing | No support | 43.0 | No support | ? | ? | ? | 43.0 |
| Allowed in sloppy mode | No support | 49.0 | 49.0 |