이 글은 편집 검토가 필요합니다. 도울을 줄 수 있는 방법을 살펴보세요.
static 키워드는 클래스의 정적 메서드를 정의합니다.
문법
static methodName() { ... }
설명
정적 메서드는 클래스의 인스턴스 없이 호출이 가능하며 클래스가 인스턴스화되면 호출할 수 없습니다. 정적 메서드는 종종 어플리케이션의 유틸리티 함수를 만드는데 사용됩니다.
예제
아래 예제는 여러가지 내용을 설명합니다. 예제에서는 어떻게 정적 메서드가 클래스에서 구현되는지 또 클래스의 정적 맴버가 서브클래스화 되는 것을 보여줍니다. 마지막으로 정적 메서드가 어떤 경우에 호출 될 수 있는지와 없는지를 설명합니다.
class Triple {
static triple(n) {
n = n || 1; //비트연산이 아니어야 합니다.
return n * 3;
}
}
class BiggerTriple extends Triple {
static triple(n) {
return super.triple(n) * super.triple(n);
}
}
console.log(Triple.triple()); // 3
console.log(Triple.triple(6)); // 18
console.log(BiggerTriple.triple(3)); // 81
var tp = new Triple();
console.log(BiggerTriple.triple(3)); // 81 (부모의 인스턴스에 영향을 받지 않습니다.)
console.log(tp.triple()); // 'tp.triple은 함수가 아닙니다.'.
명세서
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Class definitions' in that specification. |
Standard | Initial definition. |
| ECMAScript 2016 Draft (7th Edition, ECMA-262) The definition of 'Class definitions' in that specification. |
Draft |
브라우저 호환성
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 42.0 | 45 (45) | ? | ? | ? |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|
| Basic support | No support | 45.0 (45) | ? | ? | ? | 42.0 |