11
22# 정적 메서드와 정적 프로퍼티
33
4- <<<<<<< HEAD
54` "prototype" ` 이 아닌 클래스 함수 자체에 메서드를 설정할 수도 있습니다. 이런 메서드를 * 정적(static)* 메서드라고 부릅니다.
65
76정적 메서드는 아래와 같이 클래스 안에서 ` static ` 키워드를 붙여 만들 수 있습니다.
8- =======
9- We can also assign a method to the class as a whole. Such methods are called * static* .
10-
11- In a class declaration, they are prepended by ` static ` keyword, like this:
12- >>>>>>> upstream/master
137
148``` js run
159class User {
@@ -37,17 +31,9 @@ User.staticMethod(); // true
3731
3832` User.staticMethod() ` 가 호출될 때 ` this ` 의 값은 클래스 생성자인 ` User ` 자체가 됩니다(점 앞 객체).
3933
40- <<<<<<< HEAD
41- 정적 메서드는 어떤 특정한 객체가 아닌 클래스에 속한 함수를 구현하고자 할 때 주로 사용됩니다.
42-
43- 객체 ` Article ` 이 여러 개 있고 이들을 비교해줄 함수가 필요하다고 가정해 봅시다. 가장 먼저 아래와 같이 ` Article.compare ` 를 추가하는 방법이 떠오를 겁니다.
44- =======
45- Usually, static methods are used to implement functions that belong to the class as a whole, but not to any particular object of it.
46-
47- For instance, we have ` Article ` objects and need a function to compare them.
34+ 정적 메서드는 어떤 특정한 객체가 아닌 클래스 전체에 속한 함수를 구현하고자 할 때 주로 사용됩니다.
4835
49- A natural solution would be to add ` Article.compare ` static method:
50- >>>>>>> upstream/master
36+ 객체 ` Article ` 이 여러 개 있고 이들을 비교해줄 함수가 필요하다고 가정해 봅시다. 가장 먼저 아래와 같이 ` Article.compare ` 정적 메서드를 추가하는 방법이 떠오를 겁니다.
5137
5238``` js run
5339class Article {
@@ -77,29 +63,19 @@ articles.sort(Article.compare);
7763alert ( articles[0 ].title ); // CSS
7864```
7965
80- <<<<<<< HEAD
81- 여기서 ` Article.compare ` 는 article(글)을 비교해주는 수단으로, 글 전체를 '위에서' 바라보며 비교를 수행합니다. ` Article.compare ` 이 글 하나의 메서드가 아닌 클래스의 메서드여야 하는 이유가 여기에 있습니다.
66+ 여기서 ` Article.compare ` 는 article(글)을 비교해주는 수단으로, article 하나에 속한 메서드가 아니라 클래스 전체에 속한 메서드입니다.
8267
83- 이번에 살펴볼 예시는 '팩토리' 메서드를 구현한 코드입니다. 다양한 방법을 사용해 조건에 맞는 article 인스턴스를 만들어야 한다고 가정해 봅시다.
84- =======
85- Here ` Article.compare ` method stands "above" articles, as a means to compare them. It's not a method of an article, but rather of the whole class.
68+ 이번에 살펴볼 예시는 소위 '팩토리(factory)' 메서드를 구현한 코드입니다.
8669
87- Another example would be a so-called "factory" method.
88-
89- Let's say, we need multiple ways to create an article:
90- >>>>>>> upstream/master
70+ 다양한 방법을 사용해 조건에 맞는 article 인스턴스를 만들어야 한다고 가정해 봅시다.
9171
92721 . 매개변수(` title ` , ` date ` 등)를 이용해 관련 정보가 담긴 article 생성
93732 . 오늘 날짜를 기반으로 비어있는 article 생성
94743 . 기타 등등
9575
9676첫 번째 방법은 생성자를 사용해 구현할 수 있습니다. 두 번째 방법은 클래스에 정적 메서드를 만들어 구현할 수 있습니다.
9777
98- <<<<<<< HEAD
9978아래 ` Article.createTodays() ` 같이 말이죠.
100- =======
101- Such as ` Article.createTodays() ` here:
102- >>>>>>> upstream/master
10379
10480``` js run
10581class Article {
@@ -126,32 +102,21 @@ alert( article.title ); // Today's digest
126102정적 메서드는 아래 예시와 같이 항목 검색, 저장, 삭제 등을 수행해주는 데이터베이스 관련 클래스에도 사용됩니다.
127103
128104``` js
129- <<<<<< < HEAD
130105// Article은 article을 관리해주는 특별 클래스라고 가정합시다.
131106// article 삭제에 쓰이는 정적 메서드
132107Article .remove ({id: 12345 });
133108```
109+ ```` warn header="정적 메서드는 클래스에서 호출할 수 있으며, 개별 객체에서는 호출할 수 없습니다."
134110
135- ## 정적 프로퍼티
136- =======
137- // assuming Article is a special class for managing articles
138- // static method to remove the article by id:
139- Article.remove({id: 12345});
140- ```
141-
142- ````warn header="Static methods aren't available for individual objects"
143- Static methods are callable on classes, not on individual objects.
144-
145- E.g. such code won't work:
111+ 예를 들어 아래 코드는 동작하지 않습니다.
146112
147113```js
148114// ...
149115article.createTodays(); /// Error: article.createTodays is not a function
150116```
151117````
152118
153- ## Static properties
154- >>>>>>> upstream/master
119+ ## 정적 프로퍼티
155120
156121[ recent browser=Chrome]
157122
@@ -171,11 +136,7 @@ alert( Article.publisher ); // Ilya Kantor
171136Article .publisher = " Ilya Kantor" ;
172137```
173138
174- <<<<<<< HEAD
175- ## 정적 프로퍼티와 메서드 상속
176- =======
177- ## Inheritance of static properties and methods [#statics-and-inheritance]
178- >>>>>>> upstream/master
139+ ## 정적 프로퍼티와 메서드 상속 [ #statics-and-inheritance]
179140
180141정적 프로퍼티와 메서드는 상속됩니다.
181142
@@ -224,7 +185,7 @@ rabbits[0].run(); // 검은 토끼가 속도 5로 달립니다.
224185alert (Rabbit .planet ); // 지구
225186```
226187
227- 이제 `Rabbit.compare`을 호출하면 `Animal.compare`가 호출됩니다.
188+ 이제 ` Rabbit.compare ` 를 호출하면 ` Animal.compare ` 가 호출됩니다.
228189
229190이게 가능한 이유는 프로토타입 때문입니다. 이미 예상하셨겠지만, ` extends ` 키워드는 ` Rabbit ` 의 ` [[Prototype]] ` 이 ` Animal ` 을 참조하도록 해줍니다.
230191
0 commit comments