You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
어떤 사람들은 `class`라는 키워드 없이도 클래스 역할을 하는 함수를 선언할 수 있기 때문에 `클래스`는 '편의 문법'에 불과하다고 이야기합니다. 참고로 기능은 동일하나 기존 문법을 쉽게 읽을 수 있게 만든 문법을 편의 문법(syntactic sugar, 문법 설탕)이라고 합니다.
122
122
@@ -154,7 +154,7 @@ user.sayHi();
154
154
}
155
155
156
156
alert(typeof User); // function
157
-
User(); //Error: Class constructor User cannot be invoked without 'new'
157
+
User(); //TypeError: Class constructor User cannot be invoked without 'new'
158
158
```
159
159
160
160
대부분의 자바스크립트 엔진이 클래스 생성자를 문자열로 표현할 때 'class...'로 시작하는 문자열로 표현한다는 점 역시 다릅니다.
@@ -169,20 +169,20 @@ user.sayHi();
169
169
또 다른 차이점들에 대해선 더 살펴볼 예정입니다.
170
170
171
171
2. 클래스 메서드는 열거할 수 없습니다(non-enumerable).
172
-
클래스의 `"prototype"` 프로퍼티에 추가된 메서드 전체의 `enumerable` 플래그는 `false`입니다.
172
+
클래스의 `prototype` 프로퍼티에 추가된 메서드 전체의 `enumerable` 플래그는 `false`입니다.
173
173
174
-
`for..in`으로 객체를 순회할 때, 메서드는 순회 대상에서 제외하고자 하는 경우가 많음므로 이 특징은 도움이 됩니다.
174
+
`for..in`으로 객체를 순회할 때, 메서드는 순회 대상에서 제외하고자 하는 경우가 많으므로 이 특징은 꽤 유용합니다.
175
175
176
176
3. 클래스는 항상 `엄격 모드`로 실행됩니다(`use strict`).
177
177
클래스 생성자 안 코드 전체엔 자동으로 엄격 모드가 적용됩니다.
178
178
179
-
이 외에도 `class`를 사용하면 다양한 기능이 따라오는데, 자세한 내용은 곧 다루겠습니다.
179
+
이 외에도 `class`를 사용하면 다양한 기능이 따라오는데, 자세한 내용은 차차 다루겠습니다.
180
180
181
181
## 클래스 표현식
182
182
183
-
함수처럼 클래스도 다른 표현식 내부에서 정의, 전달, 반환, 할당될 수 있습니다.
183
+
함수처럼 클래스도 다른 표현식 내부에서 정의, 전달, 반환, 할당할 수 있습니다.
184
184
185
-
클래스 표현식을 만들어보겠습니다.
185
+
먼저 클래스 표현식을 만들어보겠습니다.
186
186
187
187
```js
188
188
let User = class {
@@ -207,7 +207,7 @@ let User = class *!*MyClass*/!* {
207
207
208
208
new User().sayHi(); // 제대로 동작합니다(MyClass의 정의를 보여줌).
209
209
210
-
alert(MyClass); // Error: MyClass is not defined, MyClass는 클래스 밖에서 사용할 수 없습니다.
210
+
alert(MyClass); // ReferenceError: MyClass is not defined, MyClass는 클래스 밖에서 사용할 수 없습니다.
211
211
```
212
212
213
213
아래와 같이 '필요에 따라' 클래스를 동적으로 생성하는 것도 가능합니다.
@@ -229,7 +229,7 @@ new User().sayHi(); // Hello
229
229
```
230
230
231
231
232
-
## getter·setter
232
+
## getter와 setter
233
233
234
234
리터럴을 사용해 만든 객체처럼 클래스도 getter나 setter, 계산된 프로퍼티(computed property)를 포함할 수 있습니다.
235
235
@@ -299,7 +299,7 @@ new User().sayHi();
299
299
300
300
'클래스 필드(class field)'라는 문법을 사용하면 어떤 종류의 프로퍼티도 클래스에 추가할 수 있습니다.
301
301
302
-
`class User`에 `name` 프로퍼티를 추가해봅시다.
302
+
클래스 `User`에 `name` 프로퍼티를 추가해봅시다.
303
303
304
304
```js run
305
305
class User {
@@ -315,9 +315,9 @@ class User {
315
315
new User().sayHi(); // Hello, John!
316
316
```
317
317
318
-
So, we just write "<property name> = <value>"in the declaration, and that's it.
318
+
클래스를 정의할 때 '<프로퍼티 이름> = <값>'을 써주면 간단히 클래스 필드를 만들 수 있습니다.
319
319
320
-
The important difference of class fields is that they are set on individual objects, not `User.prototype`:
320
+
클래스 필드의 중요한 특징 중 하나는 `User.prototype`이 아닌 개별 객체에만 클래스 필드가 설정된다는 점입니다.
321
321
322
322
```js run
323
323
class User {
@@ -331,26 +331,26 @@ alert(user.name); // John
331
331
alert(User.prototype.name); // undefined
332
332
```
333
333
334
-
Technically, they are processed after the constructor has done it's job, and we can use for them complex expressions and function calls:
334
+
클래스 필드는 생성자가 그 역할을 다 한 이후에 처리됩니다. 따라서 클래스 필드엔 복잡한 표현식이나 함수 호출 결과를 사용할 수 있습니다.
335
335
336
336
```js run
337
337
class User {
338
338
*!*
339
-
name = prompt("Name, please?", "John");
339
+
name = prompt("이름을 알려주세요.", "보라");
340
340
*/!*
341
341
}
342
342
343
343
let user = new User();
344
-
alert(user.name); //John
344
+
alert(user.name); // 보라
345
345
```
346
346
347
347
### Making bound methods withclassfields
348
348
349
-
As demonstrated in the chapter <info:bind>functions in JavaScript have a dynamic`this`. It depends on the context of the call.
349
+
<info:bind>챕터에서 살펴본 것처럼 자바스크립트의 함수는 동적인`this`를 갖습니다.
350
350
351
-
So if an object method is passed around and called in another context, `this` won't be a reference to its object any more.
351
+
따라서 객체 메서드를 여기저기 전달해 전혀 다른 컨텍스트에서 호출하게 되면 `this`는 원래 객체를 참조하지 않습니다.
Class fields provide a more elegant syntax for the latter solution:
401
+
그런데 클래스 필드를 사용하면 두 번째 방법을 좀 더 우아한 문법으로 처리할 수 있습니다.
402
402
403
403
```js run
404
404
class Button {
@@ -417,9 +417,9 @@ let button = new Button("hello");
417
417
setTimeout(button.click, 1000); // hello
418
418
```
419
419
420
-
The class field `click = () => {...}` creates an independent function on each `Button`object, with `this` bound to the object. Then we can pass`button.click` around anywhere, and it will be called with the right `this`.
420
+
클래스 필드 `click = () => {...}`는 각 `Button`객체마다 독립적인 함수를 만들고 함수의 `this`를 해당 객체에 바인딩시켜줍니다. 따라서 어디에 전달되던지 상관없이`button.click`은 제대로 된 `this`와 함께 호출됩니다.
421
421
422
-
That's especially useful in browser environment, when we need to setup a method as an event listener.
422
+
클래스 필드의 이런 기능은 브라우저 환경에서 메서드를 이벤트 리스너로 설정해야 할 때 특히 유용합니다.
423
423
424
424
## 요약
425
425
@@ -443,6 +443,6 @@ class MyClass {
443
443
}
444
444
```
445
445
446
-
`MyClass`는 `constructor`의 코드를 본문으로 갖는 함수입니다. `MyClass`에서 정의한 일반 메서드나 getter, setter는 `MyClass.prototype`에 쓰여집니다.
446
+
`MyClass`는 `constructor`의 코드를 본문으로 갖는 함수입니다. `MyClass`에서 정의한 일반 메서드나 getter, setter는 `MyClass.prototype`에 쓰입니다.
0 commit comments