Skip to content

Commit 0955f6e

Browse files
[프로퍼티 상속] 햄스터 문제 해답 수정
1 parent 9d01134 commit 0955f6e

File tree

1 file changed

+18
-18
lines changed
  • 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto

1 file changed

+18
-18
lines changed

1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
`speedy.eat("apple")` 호출에서 어떤 일이 일어나는지 주의 깊게 살펴봅시다.
1+
`speedy.eat("apple")`을 호출하면 무슨 일이 일어나는지 주의 깊게 알아보아야 원인을 파악할 수 있습니다.
22

3-
1. 메서드 `speedy.eat`은 프로토타입 `hamster`에서 발견됩니다. 이후 점 앞의 객체인 `speedy``this`할당되어 실행됩니다.
3+
1. 메서드 `speedy.eat`은 프로토타입 `hamster`에서 발견되는데, 점 앞엔 객체 `speedy`있으므로 `this``speedy`할당되어 메서드가 실행됩니다.
44

5-
2. `this.stomach.push()`를 실행하려면 프로퍼티 `stomach`을 찾아서 여기에 `push`를 호출해야 합니다. `this`(`=speedy`)에 `stomach` 있는지 찾아보았지만, 아무것도 발견할 수 없습니다.
5+
2. `this.stomach.push()`를 실행하려면 프로퍼티 `stomach`을 찾아서 여기에 `push`를 호출해야 합니다. 그런데 `this``speedy`엔 프로퍼티 `stomach`이 없습니다.
66

7-
3. 프로토타입 체인을 거슬러 올라가 `hamster`에서 `stomach` 발견합니다.
7+
3. `stomach`을 찾기위해 프로토타입 체인을 거슬러 올라가보니 `hamster` `stomach`이 있는것을 발견합니다.
88

9-
4. `stomach`을 대상으로 `push`를 호출하여 *프로토타입(`hamster`)의 stomach* 에 food를 추가합니다.
9+
4. `push` 메서드는 *프로토타입 `hamster`에 있는 stomach* 을 대상으로 동작하여 프로토타입에 food가 추가됩니다.
1010

11-
모든 햄스터가 하나의 stomach를 공유하는 이유가 바로 여기에 있습니다.
11+
모든 햄스터가 하나의 stomach를 공유하는 이유는 바로 이런 동작방식 때문입니다.
1212

13-
`lazy.stomach.push(...)` `speedy.stomach.push()`를 호출했을 때 모두, 프로퍼티 `stomach` 프로토타입에서 발견합니다(`stomach`이 객체 자체에 있지 않기 때문입니다). 따라서 새로운 데이터는 `stomach`에 추가됩니다.
13+
`lazy.stomach.push(...)`, `speedy.stomach.push()`를 호출했을 때 모두 프로퍼티 `stomach` 프로토타입에서 발견됩니다. 따라서 새로운 데이터는 `stomach`에 추가됩니다.
1414

15-
`this.stomach=`이용해 데이터를 할당하면 이런 일이 발생하지 않습니다.
15+
문제를 해결하려면 `push` 메서드가 아닌 `this.stomach=`사용해 데이터를 할당하면 됩니다.
1616

1717
```js run
1818
let hamster = {
1919
stomach: [],
2020

2121
eat(food) {
2222
*!*
23-
// this.stomach.push 대신에 this.stomach에 할당
23+
// this.stomach.push(food) 대신에 food를 this.stomach에 할당
2424
this.stomach = [food];
2525
*/!*
2626
}
@@ -34,17 +34,17 @@ let lazy = {
3434
__proto__: hamster
3535
};
3636

37-
// Speedy는 음식을 발견합니다.
37+
// 햄스터 speedy가 음식을 먹습니다.
3838
speedy.eat("apple");
3939
alert( speedy.stomach ); // apple
4040

41-
// Lazy의 stomach는 비어있습니다.
42-
alert( lazy.stomach ); // <nothing>
41+
// lazy는 음식을 먹지 않았기 때문에 배가 비어있습니다.
42+
alert( lazy.stomach ); // (아무것도 출력 안됨)
4343
```
4444

45-
`this.stomach=``stomach`을 찾지 않기 때문에 의도한 대로 잘 작동합니다. 새로운 데이터는 `this`가 가리키는 객체에 바로 쓰입니다.
45+
`this.stomach=`은 객체 자체에 해당 프로퍼티를 추가하지 프로토타입 체인에서 `stomach`을 찾지 않기 때문에 의도한 대로 잘 작동합니다.
4646

47-
외에도 햄스터가 각자의 stomach를 가지게 하면 문제를 완전히 피할 수 있습니다.
47+
방법 말고도 햄스터가 각자의 stomach를 가지게 하면 문제를 사전에 차단할 수 있습니다.
4848

4949
```js run
5050
let hamster = {
@@ -69,12 +69,12 @@ let lazy = {
6969
*/!*
7070
};
7171

72-
// speedy는 음식을 발견합니다.
72+
// 햄스터 speedy가 음식을 먹습니다.
7373
speedy.eat("apple");
7474
alert( speedy.stomach ); // apple
7575

76-
// lazy의 stomach은 비어있습니다.
77-
alert( lazy.stomach ); // <nothing>
76+
// lazy는 음식을 먹지 않았기 때문에 배가 비어있습니다.
77+
alert( lazy.stomach ); // (아무것도 출력 안됨)
7878
```
7979

80-
`stomach`과 같이 특정 객체의 상태를 나타내는 프로퍼티 전부를 해당 객체 안에서 작성하는 것은 이런 문제를 예방할 수 있는 일반적인 해결 방법입니다.
80+
문제의 `stomach`처럼 특정 객체의 상태를 설명하는 프로퍼티는 조상 객체가 아닌 객체 자체에 정의하는 것이 이런 문제를 차단할 수 있는 일반적인 방법입니다.

0 commit comments

Comments
 (0)