From fb78901f70d8becd267723b25fcdb901adc189fd Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 8 Nov 2019 10:22:07 +0900 Subject: [PATCH 1/2] =?UTF-8?q?#359=20[=ED=95=A8=EC=88=98=EC=9D=98=20proto?= =?UTF-8?q?type=20=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0]=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=9C1=20=EC=A0=95=EB=8B=B5=20=EB=B2=88=EC=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1-changing-prototype/solution.md | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/1-js/08-prototypes/02-function-prototype/1-changing-prototype/solution.md b/1-js/08-prototypes/02-function-prototype/1-changing-prototype/solution.md index ebbdf3a7c1..8e6c75c1f3 100644 --- a/1-js/08-prototypes/02-function-prototype/1-changing-prototype/solution.md +++ b/1-js/08-prototypes/02-function-prototype/1-changing-prototype/solution.md @@ -1,20 +1,44 @@ +`"constructor"` ÇÁ·ÎÆÛƼ¿¡ Á¦´ë·Î µÈ °ªÀÌ ÀúÀåµÇ¾îÀÖ´Ù¸é À§¿Í °°Àº Á¢±Ù¹ýÀÌ °¡´ÉÇÕ´Ï´Ù. -Answers: +±âº» `"prototype"`¸¦ º¯°æÇÏÁö ¾Ê¾Ò´Ù¸é ¾Æ·¡ ¿¹½Ã´Â ÀǵµÇÑ ´ë·Î µ¿ÀÛÇÕ´Ï´Ù. -1. `true`. +```js run +function User(name) { + this.name = name; +} - The assignment to `Rabbit.prototype` sets up `[[Prototype]]` for new objects, but it does not affect the existing ones. +let user = new User('John'); +let user2 = new user.constructor('Pete'); -2. `false`. +alert( user2.name ); // Pete (Àß µ¿ÀÛÇϳ׿ä!) +``` - Objects are assigned by reference. The object from `Rabbit.prototype` is not duplicated, it's still a single object referenced both by `Rabbit.prototype` and by the `[[Prototype]]` of `rabbit`. +`User.prototype.constructor == User`À̱⠶§¹®¿¡ À§ ¿¹½Ã´Â Á¦´ë·Î µ¿ÀÛÇÕ´Ï´Ù. - So when we change its content through one reference, it is visible through the other one. +±×·±µ¥ ´©±º°¡°¡ `User.prototype`¸¦ µ¤¾î¾²°í `User`¸¦ ÂüÁ¶ÇÏ´Â `constructor`¸¦ ´Ù½Ã ¸¸µé¾îÁÖ´Â °É Àؾú´Ù¸é ¹®Á¦ÀÇ Á¢±Ù¹ýÀº ½ÇÆÐÇÕ´Ï´Ù. -3. `true`. +¿¹½Ã: - All `delete` operations are applied directly to the object. Here `delete rabbit.eats` tries to remove `eats` property from `rabbit`, but it doesn't have it. So the operation won't have any effect. +```js run +function User(name) { + this.name = name; +} +*!* +User.prototype = {}; // (*) +*/!* -4. `undefined`. +let user = new User('John'); +let user2 = new user.constructor('Pete'); - The property `eats` is deleted from the prototype, it doesn't exist any more. +alert( user2.name ); // undefined +``` + +¿Ö `user2.name`ÀÌ `undefined`°¡ µÉ±î¿ä? + +±× ÀÌÀ¯´Â `new user.constructor('Pete')`°¡ ¾Æ·¡¿Í °°ÀÌ µ¿ÀÛÇϱ⠶§¹®ÀÔ´Ï´Ù. + +1. `new user.constructor('Pete')`´Â `user`¿¡¼­ `constructor`¸¦ ã´Âµ¥ ¾Æ¹«°Íµµ ãÁö ¸øÇÕ´Ï´Ù. +2. °´Ã¼¿¡¼­ ¿øÇÏ´Â ÇÁ·ÎÆÛƼ¸¦ ãÁö ¸øÇ߱⠶§¹®¿¡ ÇÁ·ÎÅäŸÀÔ¿¡¼­ °Ë»öÀ» ½ÃÀÛÇÕ´Ï´Ù. `user`ÀÇ ÇÁ·ÎÅäŸÀÔÀº `User.prototype`Àε¥, `User.prototype`Àº ºó °´Ã¼ÀÔ´Ï´Ù. +3. `User.prototype`Àº ÀÏ¹Ý °´Ã¼ `{}`À̰í, ÀÏ¹Ý °´Ã¼ÀÇ ÇÁ·ÎÅäŸÀÔÀº `Object.prototype`ÀÔ´Ï´Ù. `Object.prototype.constructor == Object`À̹ǷΠ`Object`°¡ »ç¿ëµË´Ï´Ù. + +°á±¹¿¡ `let user2 = new user.constructor('Pete');`´Â `let user2 = new Object('Pete')`°¡ µË´Ï´Ù. ±×·±µ¥ `Object`ÀÇ »ý¼ºÀÚ´Â Àμö¸¦ ¹«½ÃÇϰí Ç×»ó ºó °´Ã¼¸¦ »ý¼ºÇÕ´Ï´Ù. µû¶ó¼­ `let user2 = new Object('Pete')`´Â `let user2 = {}`¿Í °°´Ù°í »ý°¢ÇÒ ¼ö ÀÖ½À´Ï´Ù. `user2.name`ÀÌ `undefined`ÀÎ ÀÌÀ¯°¡ ¿©±â¿¡ ÀÖ½À´Ï´Ù. \ No newline at end of file From d1a6c942996ff3ad36eabf4e870083178281a740 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 8 Nov 2019 11:11:44 +0900 Subject: [PATCH 2/2] =?UTF-8?q?#359[=ED=95=A8=EC=88=98=EC=9D=98=20prototyp?= =?UTF-8?q?e=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0]=EA=B3=BC=EC=A0=9C1=20?= =?UTF-8?q?=EC=A0=95=EB=8B=B5=20=EB=B2=88=EC=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../02-function-prototype/1-changing-prototype/solution.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/08-prototypes/02-function-prototype/1-changing-prototype/solution.md b/1-js/08-prototypes/02-function-prototype/1-changing-prototype/solution.md index 8e6c75c1f3..b73b6c9354 100644 --- a/1-js/08-prototypes/02-function-prototype/1-changing-prototype/solution.md +++ b/1-js/08-prototypes/02-function-prototype/1-changing-prototype/solution.md @@ -5,7 +5,7 @@ ```js run function User(name) { this.name = name; -} +}; let user = new User('John'); let user2 = new user.constructor('Pete'); @@ -22,7 +22,7 @@ alert( user2.name ); // Pete ( ```js run function User(name) { this.name = name; -} +}; *!* User.prototype = {}; // (*) */!*