Skip to content

Commit d798415

Browse files
lumosmindViolet-Bora-Lee
authored andcommitted
clarifying example code
An interesting question may arise in the example above: what’s the value of this inside set fullName(value)? Where are the properties this.name and this.surname written: into user or admin? The answer is simple: this is not affected by prototypes at all. No matter where the method is found: in an object or its prototype. In a method call, this is always the object before the dot. So, the setter call admin.fullName= uses admin as this, not user. That is actually a super-important thing, because we may have a big object with many methods, and have objects that inherit from it. And when the inheriting objects run the inherited methods, they will modify only their own states, not the state of the big object. The example code doesn't show these concepts. Also, these additions can make readers ask questions before the explanation of the example code.
1 parent b77bc4e commit d798415

File tree

1 file changed

+3
-0
lines changed
  • 1-js/08-prototypes/01-prototype-inheritance

1 file changed

+3
-0
lines changed

1-js/08-prototypes/01-prototype-inheritance/article.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ alert(admin.fullName); // John Smith (*)
198198

199199
// setter 함수가 실행됩니다!
200200
admin.fullName = "Alice Cooper"; // (**)
201+
202+
alert(admin.fullName); // Alice Cooper , state of admin modified
203+
alert(user.fullName); // John Smith , state of user protected
201204
```
202205

203206
`(*)`로 표시한 줄에서 `admin.fullName`은 프로토타입(`user`)에 있는 getter 함수(`get fullName`)를 호출하고, `(**)`로 표시한 줄의 할당 연산은 프로토타입에 있는 setter 함수(`set fullName`)를 호출합니다.

0 commit comments

Comments
 (0)