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..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 @@ -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