|
1 | 1 |
|
2 | | -We can see which class it belongs by outputting it, like: |
| 2 | +`document` 노드가 어떤 클래스에 속해있는지는 아래와 같은 스크립트를 통해 확인할 수 있습니다. |
3 | 3 |
|
4 | 4 | ```js run |
5 | 5 | alert(document); // [object HTMLDocument] |
6 | 6 | ``` |
7 | 7 |
|
8 | | -Or: |
| 8 | +아래 스크립트로도 확인할 수 있습니다. |
9 | 9 |
|
10 | 10 | ```js run |
11 | 11 | alert(document.constructor.name); // HTMLDocument |
12 | 12 | ``` |
13 | 13 |
|
14 | | -So, `document` is an instance of `HTMLDocument` class. |
| 14 | +살펴본 바와 같이 `document`는 `HTMLDocument` 클래스의 인스턴스입니다. |
15 | 15 |
|
16 | | -What's its place in the hierarchy? |
| 16 | +그렇다면 `HTMLDocument` 클래스는 DOM 계층 구조에서 어디에 있을까요? |
17 | 17 |
|
18 | | -Yeah, we could browse the specification, but it would be faster to figure out manually. |
| 18 | +명세서를 확인하면 알 수 있긴 하지만, 직접 만든 코드를 사용하면 위치를 더 빨리 알아낼 수 있습니다. |
19 | 19 |
|
20 | | -Let's traverse the prototype chain via `__proto__`. |
| 20 | +`__proto__`를 사용해 프로토타입 체인을 거슬러 올라가 봅시다. |
21 | 21 |
|
22 | | -As we know, methods of a class are in the `prototype` of the constructor. For instance, `HTMLDocument.prototype` has methods for documents. |
| 22 | +아시다시피 클래스의 메서드는 생성자의 `prototype`에 구현되어있습니다. `document`의 메서드는 `HTMLDocument.prototype`에 구현되어 있죠. |
23 | 23 |
|
24 | | -Also, there's a reference to the constructor function inside the `prototype`: |
| 24 | +여기에 더하여 `prototype` 안에는 생성자 함수의 참조 역시 저장되어 있습니다. 이를 확인해 봅시다. |
25 | 25 |
|
26 | 26 | ```js run |
27 | 27 | alert(HTMLDocument.prototype.constructor === HTMLDocument); // true |
28 | 28 | ``` |
29 | 29 |
|
30 | | -To get a name of the class as a string, we can use `constructor.name`. Let's do it for the whole `document` prototype chain, till class `Node`: |
| 30 | +클래스 이름을 얻으려면 `constructor.name`를 사용하면 됩니다. `Node` 클래스를 만날 때까지 `document`의 프로토타입 체인 전체를 거슬러 올라가 보겠습니다. |
31 | 31 |
|
32 | 32 | ```js run |
33 | 33 | alert(HTMLDocument.prototype.constructor.name); // HTMLDocument |
34 | 34 | alert(HTMLDocument.prototype.__proto__.constructor.name); // Document |
35 | 35 | alert(HTMLDocument.prototype.__proto__.__proto__.constructor.name); // Node |
36 | 36 | ``` |
37 | 37 |
|
38 | | -That's the hierarchy. |
| 38 | +위와 같은 코드를 사용해 계층 구조를 파악할 수 있습니다. |
39 | 39 |
|
40 | | -We also could examine the object using `console.dir(document)` and see these names by opening `__proto__`. The console takes them from `constructor` internally. |
| 40 | +이 외에도 개발자 도구에서 `console.dir(document)`를 사용해 객체를 검사하고, `__proto__`에 저장된 정보를 통해 이름을 알아낼 수도 있습니다. 브라우저 콘솔 내부에서 자동으로 `constructor`의 이름을 추출하기 때문입니다. |
0 commit comments