Too soon!
가장 최근에 나온 방식으로 아직 Safari 에서도 사용이 불가하여 BABEL을 써야한다.
static 은 선언된 object 마다 할당되어지는 것이 아니라, class 자체에 할당되어지는 개념이다.
그러므로 static이 선언된 변수나 메소드는 object로 호출하는 것이 아닌 클래스로 호출되어야 한다.
이러한 방식은 object에 상관없이, 들어오는 데이터에 상관없이 공통적으로 class에서 사용되는 것이라면
static variable, static method 를 사용한다.
그래야 효율적으로 메모리를 사용할 수 있기 때문이다.
class Article {
static publisher = 'Dream Coding'; // static variable
constructor(articleNumber) {
this.articleNumber = articleNumber;
}
static printPublisher() { // static method
console.log(Article.publisher);
}
}
const article1 = new Article(1);
const article2 = new Article(2);
console.log(article1.articleNumber); // 1
console.log(article2.publisher); // undefined (static이라 object 할당 x)
console.log(Article.publisher); // Dream Coding
article1.printPublisher(); // (Error)
Article.printPublisher(); // Dream Coding