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
Copy file name to clipboardExpand all lines: 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
-
`speedy.eat("apple")` 호출에서 어떤 일이 일어나는지 주의 깊게 살펴봅시다.
1
+
`speedy.eat("apple")`을 호출하면 무슨 일이 일어나는지 주의 깊게 알아보아야 원인을 파악할 수 있습니다.
2
2
3
-
1. 메서드 `speedy.eat`은 프로토타입 `hamster`에서 발견됩니다. 이후 점 앞의 객체인`speedy`가 `this`에 할당되어 실행됩니다.
3
+
1. 메서드 `speedy.eat`은 프로토타입 `hamster`에서 발견되는데, 점 앞엔 객체`speedy`가 있으므로 `this`엔 `speedy`가 할당되어 메서드가 실행됩니다.
4
4
5
-
2.`this.stomach.push()`를 실행하려면 프로퍼티 `stomach`을 찾아서 여기에 `push`를 호출해야 합니다. `this`(`=speedy`)에 `stomach`이 있는지 찾아보았지만, 아무것도 발견할 수 없습니다.
5
+
2.`this.stomach.push()`를 실행하려면 프로퍼티 `stomach`을 찾아서 여기에 `push`를 호출해야 합니다. 그런데 `this`인 `speedy`엔 프로퍼티 `stomach`이 없습니다.
6
6
7
-
3. 프로토타입 체인을 거슬러 올라가`hamster`에서`stomach`를 발견합니다.
7
+
3.`stomach`을 찾기위해 프로토타입 체인을 거슬러 올라가보니`hamster`에`stomach`이 있는것을 발견합니다.
8
8
9
-
4.`stomach`을 대상으로 `push`를 호출하여*프로토타입(`hamster`)의 stomach*에 food를 추가합니다.
9
+
4.`push` 메서드는*프로토타입`hamster`에 있는 stomach*을 대상으로 동작하여 프로토타입에 food가 추가됩니다.
10
10
11
-
모든 햄스터가 하나의 stomach를 공유하는 이유가 바로 여기에 있습니다.
11
+
모든 햄스터가 하나의 stomach를 공유하는 이유는 바로 이런 동작방식 때문입니다.
12
12
13
-
`lazy.stomach.push(...)`와`speedy.stomach.push()`를 호출했을 때 모두, 프로퍼티 `stomach`을 프로토타입에서 발견합니다(`stomach`이 객체 자체에 있지 않기 때문입니다). 따라서 새로운 데이터는 `stomach`에 추가됩니다.
13
+
`lazy.stomach.push(...)`,`speedy.stomach.push()`를 호출했을 때 모두 프로퍼티 `stomach`은 프로토타입에서 발견됩니다. 따라서 새로운 데이터는 `stomach`에 추가됩니다.
14
14
15
-
`this.stomach=`을 이용해 데이터를 할당하면 이런 일이 발생하지 않습니다.
15
+
문제를 해결하려면 `push` 메서드가 아닌 `this.stomach=`을 사용해 데이터를 할당하면 됩니다.
16
16
17
17
```js run
18
18
let hamster = {
19
19
stomach: [],
20
20
21
21
eat(food) {
22
22
*!*
23
-
// this.stomach.push 대신에 this.stomach에 할당
23
+
// this.stomach.push(food) 대신에 food를 this.stomach에 할당
24
24
this.stomach= [food];
25
25
*/!*
26
26
}
@@ -34,17 +34,17 @@ let lazy = {
34
34
__proto__: hamster
35
35
};
36
36
37
-
//Speedy는 음식을 발견합니다.
37
+
//햄스터 speedy가 음식을 먹습니다.
38
38
speedy.eat("apple");
39
39
alert( speedy.stomach ); // apple
40
40
41
-
//Lazy의 stomach는 비어있습니다.
42
-
alert( lazy.stomach ); //<nothing>
41
+
//lazy는 음식을 먹지 않았기 때문에 배가 비어있습니다.
42
+
alert( lazy.stomach ); //(아무것도 출력 안됨)
43
43
```
44
44
45
-
`this.stomach=`는 `stomach`을 찾지 않기 때문에 의도한 대로 잘 작동합니다. 새로운 데이터는 `this`가 가리키는 객체에 바로 쓰입니다.
45
+
`this.stomach=`은 객체 자체에 해당 프로퍼티를 추가하지 프로토타입 체인에서 `stomach`을 찾지 않기 때문에 의도한 대로 잘 작동합니다.
46
46
47
-
이 외에도 햄스터가 각자의 stomach를 가지게 하면 이 문제를 완전히 피할 수 있습니다.
47
+
이 방법 말고도 햄스터가 각자의 stomach를 가지게 하면 문제를 사전에 차단할 수 있습니다.
48
48
49
49
```js run
50
50
let hamster = {
@@ -69,12 +69,12 @@ let lazy = {
69
69
*/!*
70
70
};
71
71
72
-
//speedy는 음식을 발견합니다.
72
+
//햄스터 speedy가 음식을 먹습니다.
73
73
speedy.eat("apple");
74
74
alert( speedy.stomach ); // apple
75
75
76
-
//lazy의 stomach은 비어있습니다.
77
-
alert( lazy.stomach ); //<nothing>
76
+
//lazy는 음식을 먹지 않았기 때문에 배가 비어있습니다.
77
+
alert( lazy.stomach ); //(아무것도 출력 안됨)
78
78
```
79
79
80
-
`stomach`과 같이 특정 객체의 상태를 나타내는 프로퍼티 전부를 해당 객체 안에서 작성하는 것은 이런 문제를 예방할 수 있는 일반적인 해결 방법입니다.
80
+
문제의 `stomach`처럼 특정 객체의 상태를 설명하는 프로퍼티는 조상 객체가 아닌 객체 자체에 정의하는 것이 이런 문제를 차단할 수 있는 일반적인 방법입니다.
0 commit comments