From 1fe15f28f32ff8daea5486240a0572f8d808c95e Mon Sep 17 00:00:00 2001
From: eve712 <62237639+eve712@users.noreply.github.com>
Date: Sun, 15 Nov 2020 01:31:01 +0900
Subject: [PATCH] =?UTF-8?q?Delete=20=ED=95=99=EC=8A=B5=EC=A0=95=EB=A6=AC(p?=
=?UTF-8?q?rototype,class).md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4\240\225\353\246\254(prototype,class).md" | 187 ------------------
1 file changed, 187 deletions(-)
delete mode 100644 "Mission2/\355\225\231\354\212\265\354\240\225\353\246\254(prototype,class).md"
diff --git "a/Mission2/\355\225\231\354\212\265\354\240\225\353\246\254(prototype,class).md" "b/Mission2/\355\225\231\354\212\265\354\240\225\353\246\254(prototype,class).md"
deleted file mode 100644
index f0ad21e..0000000
--- "a/Mission2/\355\225\231\354\212\265\354\240\225\353\246\254(prototype,class).md"
+++ /dev/null
@@ -1,187 +0,0 @@
-# โ๏ธPrototype
-
-## ๐ Prototype = Prototype Object + Prototype Link
-1. Prototype property๊ฐ ๊ฐ๋ฆฌํค๋ `Prototype Object`
-2. ์๊ธฐ ์์ ์ ๋ง๋ค์ด๋ธ ๊ฐ์ฒด์ ์ํ์ ์๋ฏธํ๋ `Prototype Link`
-
-
-
-## ๐ Prototype Object(Prototype property)
-ํ๋กํ ํ์
์ ์๊ธฐ ์์ ์ ์์ฑํ๊ฒ ํ ์์ ์ ์ํ ๊ฐ์ฒด์ด๋ค. ์ํ ๊ฐ์ฒด๋ผ๋ ํ๋กํ ํ์
์ function A() ํจ์ ๊ฐ์ฒด ๊ทธ ์์ฒด์ผ๊น? ์๋๋ค!!
-
-์๋ฐ์คํฌ๋ฆฝํธ์ ๋ชจ๋ ๊ฐ์ฒด๋ ์์ฑ๊ณผ ๋์์ ์๊ธฐ์์ ์ด ์์ฑ๋ ๋น์์ ์ ๋ณด๋ฅผ ์ทจํ `Prototype Object`๋ผ๋ ์๋ก์ด ๊ฐ์ฒด๋ฅผ Cloningํด์ ๋ง๋ค์ด๋ธ๋ค. `prototype`์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด๋ด๊ธฐ ์ํ ์ํ์ด๋ผ๋ฉด ์ด `Prototype Object`๋ **์๊ธฐ ์์ ์ ๋ถ์ ์ด๋ฉฐ ์์ ์ ์ํ์ผ๋ก ๋ง๋ค์ด์ง ๋ค๋ฅธ ๊ฐ์ฒด๊ฐ ์ฐธ์กฐํ ํ๋กํ ํ์
์ด ๋๋ค.** ์ฆ ๊ฐ์ฒด ์์ ์ ์ด์ฉํ ๋ค๋ฅธ ๊ฐ์ฒด๋ค์ด ํ๋กํ ํ์
์ผ๋ก ์ฌ์ฉํ ๊ฐ์ฒด๊ฐ Prototype Object์ธ ๊ฒ!
-
-
-
-< ํจ์๊ฐ ์ ์๋ ๋ ์ด๋ฃจ์ด์ง๋ ์ผ๋ค! >
-1. ํด๋น ํจ์์ Constructor(์์ฑ์) ์๊ฒฉ ๋ถ์ฌ
-2. ํด๋น ํจ์์ Prototype Object ์์ฑ, ์ฐ๊ฒฐ
-
-ํจ์๋ฅผ ์ ์ํ๋ฉด ํจ์๋ง ์์ฑ๋๋ ๊ฒ์ด ์๋๋ผ Prototype Object๋ ๊ฐ์ด ์์ฑ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์์ฑ๋ ํจ์๋ `prototype`์ด๋ผ๋ ์์ฑ์ ํตํด `Prototype Object`์ ์ ๊ทผํ ์ ์๋ค. Prototype Object๋ ์ผ๋ฐ์ ์ธ ๊ฐ์ฒด์ ๊ฐ์ผ๋ฉฐ ๊ธฐ๋ณธ์ ์ธ ์์ฑ์ผ๋ก `constructor`์ `__proto__`๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ๋ํ Prototype Object๋ ์ผ๋ฐ์ ์ธ ๊ฐ์ฒด์ด๋ฏ๋ก ์์ฑ์ ๋ง์๋๋ก ์ถ๊ฐ/์ญ์ ํ ์ ์๋ค.
-```javascript
-Person.prototype.eyes = 2;
-Person.prototype.nose = 1;
-// prototype ์์ฑ์ผ๋ก Prototype Object์ ์ ๊ทผํด ์์ฑ์ ์ถ๊ฐํ๋ค.
-```
-
-
-
-## ๐ Prototype Link(`__proto__`)
-```javascript
-function Person() {}
-
-Person.prototype.eyes = 2;
-Person.prototype.nose = 1;
-
-let kim = new Person();
-let park = new Person();
-
-console.log(kim); // Person { }
-console.log(kim.eyes); // 2
-```
-kim์๋ eyes๋ผ๋ ์์ฑ์ด ์๋๋ฐ๋ kim.eyes๋ฅผ ์คํํ๋ฉด 2๋ผ๋ ๊ฐ์ ์ฐธ์กฐํ๋ค. Prototype Object์ ์กด์ฌํ๋ eyes ์์ฑ์ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์ด๋ค. ์ด๊ฒ์ด ๊ฐ๋ฅํ ์ด์ ๋ kim์ด ๊ฐ์ง๊ณ ์๋ ๋ฑ ํ๋์ ์์ฑ `__proto__`๊ฐ ๊ทธ๊ฒ์ ๊ฐ๋ฅํ๊ฒ ํด์ฃผ๊ธฐ ๋๋ฌธ์ด๋ค.
-
-`__proto__` ์์ฑ์ ๋ชจ๋ ๊ฐ์ฒด๊ฐ ๋น ์ง์์ด ๊ฐ์ง๊ณ ์๋ ์์ฑ์ผ๋ก **๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ์กฐ์์ด์๋ ํจ์์ Prototype Object๋ฅผ ๊ฐ๋ฆฌํจ๋ค.** kim๊ฐ์ฒด๋ Personํจ์๋ก๋ถํฐ ์์ฑ๋์์ผ๋ Person ํจ์์ Prototype Object๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ ๊ฒ!
-
-kim๊ฐ์ฒด๊ฐ eyes๋ฅผ ์ง์ ๊ฐ์ง๊ณ ์์ง ์๊ธฐ ๋๋ฌธ์ eyes ์์ฑ์ ์ฐพ์ ๋๊น์ง ์์ ํ๋กํ ํ์
์ ํ์ํ๋ค. ์ต์์์ธ Object์ Prototype Object๊น์ง ๋๋ฌํ๋๋ฐ๋ ๋ชป ์ฐพ์์ ๊ฒฝ์ฐ undefined๋ฅผ ๋ฆฌํดํ๋ค. ์ด๋ ๊ฒ `__proto__`์์ฑ์ ํตํด ์์ ํ๋กํ ํ์
๊ณผ ์ฐ๊ฒฐ๋์ด ์๋ ํํ๋ฅผ ํ๋กํ ํ์
์ฒด์ธ(Chain)์ด๋ผ๊ณ ํ๋ค.
-
-์ด๋ฐ ํ๋กํ ํ์
์ฒด์ธ ๊ตฌ์กฐ๋๋ฌธ์ ๋ชจ๋ ๊ฐ์ฒด๋ Object์ ์์์ด๋ผ๊ณ ๋ถ๋ฆฌ๊ณ , Object Prototype Object์ ์๋ ๋ชจ๋ ์์ฑ์ ์ฌ์ฉํ ์ ์๋ค. ์๋ฅผ ๋ค๋ฉด toStringํจ์๊ฐ ์๋ค.
-
-๋ชจ๋ ๊ฐ์ฒด์ ํ์ฅ์ ๊ฐ์ฒด๊ฐ ์์ ํ prototype Object๋ฅผ ํตํด ์ด๋ฃจ์ด์ง๋ฉฐ ์ด ์ฐ๊ฒฐ์ ๋์ Object ๊ฐ์ฒด์ prototype Object์ด๋ค.
-
-
-
-
-## ๐ ์์๋ก ์ดํดํ๊ธฐ
-
-```javascript
-function foo(value) {
- this.x = value;
-}
-let A = new foo('hello');
-console.log(A.x); //hello
-console.log(A.prototype.x) //syntax error
-```
-1. prototype ํ๋กํผํฐ๋ Constructor๊ฐ ๊ฐ์ง๋ ํ๋กํผํฐ๋ค. **prototype์ ํจ์๊ฐ์ฒด๋ง ๊ฐ์ง๊ณ ์๋ ์์ฑ!!**
-2. ์ฌ๊ธฐ์ A๊ฐ์ฒด๋ ํจ์๊ฐ์ฒด๊ฐ ์๋๊ณ , ํจ์๊ฐ์ฒด๋ฅผ ํตํด ๋ง๋ค์ด์ง ๋จ์ผ ๊ฐ์ฒด์ผ ๋ฟ์ด๋ค. ์ฆ, A๋ prototype ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ง ์๊ธฐ ๋๋ฌธ์ A.prototype.x๊ฐ syntax error์ธ ๊ฒ์ด๋ค.
-3. foo.prototype.x๋ ๋ง์ง๋ง A.prototype.x๋ ์๋ฌ!
-
-
-
-```javascript
-let A = function () {};
-A.prototype.x = function() {
- console.log('hello');
-};
-let B = new A();
-B.x();
- // hello
-console.log(A.prototype);
- // x: f ()
- // constructor: f () โ Prototype Object ๊ธฐ๋ณธ ์์ฑ
- // __proto__: Object โ Prototype Object ๊ธฐ๋ณธ ์์ฑ
-```
-1. A ํจ์์ prototype์์ฑ์ด `A Prototype Object`์ ์ ๊ทผํด x๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ค.
-2. B ๊ฐ์ฒด๋ ๋จ ํ๋์ ์์ฑ `__proto__`๋ฅผ ๊ฐ๊ณ ์๊ณ , ์ด ์์ฑ์ `A Prototype Object`์ ์ ๊ทผํด x๋ฅผ ์ฐธ์กฐํ๋ค.
-3. A.prototype์ผ๋ก A Prototype Object๋ฅผ ๋ณด๋ฉด, ๊ธฐ๋ณธ ์์ฑ์ธ constructor(Aํจ์), \__proto__์ ๊ฐ์ง๊ณ ์๊ณ , ์ถ๊ฐํ x๋ฉ์๋๋ ๊ฐ์ง๊ณ ์๋ค.
-4. `__proto__`๋ prototype link๋ก ์์์์ ๋ฌผ๋ ค๋ฐ์ ๊ฐ์ฒด์ ํ๋กํ ํ์
์ ๋ํ ์ ๋ณด์ด๋ค. A Prototype Object์ ์์ ๊ฐ์ฒด๋ Object์ด๊ธฐ ๋๋ฌธ์ \__proto__ ์์ฑ๊ฐ์ผ๋ก Object๊ฐ ์๋๊ฒ์ด๋ค.
-5. Aํจ์๊ฐ ๋น์ด์๊ธฐ ๋๋ฌธ์ A Prototype Object์ constructor๊ฐ ๋น์ด์๋ค. A.prototype.x = function() {...}; ์ด๋ ๊ฒ ํ๋ค๊ณ ํด์ A Prototype Object์ constructor ์์ฑ์ด ์ฑ์์ง์ง๋ ์๋๋ค. constructor๋ Aํจ์๊ฐ ์์ฑ๋๋ ๋น์์ ์ ๋ณด๋ง์ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ ๋ณ๊ฒฝ์ด ๋ถ๊ฐ๋ฅํ๋ค. A.prototype.x๋ A Prototype Object์ ์๋ก์ด x๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ B.x();๊ฐ ์คํ์ด ๋๋ค.
-6. constructor์ ๋ด์ฉ์ ๋ณ๊ฒฝ์ด ๋ถ๊ฐ(์ฌํ ๋นX)ํ์ง๋ง, ์์ ๊ฒฝ์ฐ์ฒ๋ผ A.prototype.x๋ก ๋ฐ์์ ๋ฉ์๋๋ฅผ Prototype Object์ ์ถ๊ฐํ ๊ฒฝ์ฐ์๋, A.prototype.x๋ก ์ฌ์ ์ํ ์ ์๋ค! **์ฆ, Prototype Object์ ๊ธฐ๋ณธ ์์ฑ์ธ constructor๋ก ๋ค์ด๊ฐ๋์ง, Prototype Object์ ๋ฉ์๋๋ก ์๋ก ์ถ๊ฐ๊ฐ ๋๋ ๊ฒ์ธ์ง์ ๋ฐ๋ผ ์ฌ์ ์ ๊ฐ๋ฅ์ฌ๋ถ๊ฐ ๋ค๋ฅด๋ค๊ณ ํ ์ ์๋ค!**
-
-
-
-๋ง์ฝ์ constructor์ x๋ฉ์๋๊ฐ ์๊ณ , ์์ฑ์ ํจ์ ๋ฐ์์ ๋ A.prototype.x๋ฅผ ์ด๋ค๋ฉด ์ด๋ป๊ฒ ๋ ๊น? ๋ฐ์ ์์ ์ฐธ๊ณ !! ๊ฒฐ๊ณผ๋ฅผ ๋จผ์ ๋งํ์๋ฉด, B.x();๋ constructor์ ์๋ x๋ฉ์๋๋ฅผ ์คํํ๋ค.
-
-```javascript
-let A = function () {
- this.x = function () {
- console.log('hello');
- };
-};
-A.prototype.x = function() {
- console.log('world');
-};
-let B = new A();
-
-B.x(); //hello
-console.log(A.prototype); // {x: f, constructor: f}
-A.prototype.x(); // world
-B.__proto__.x(); //world
-```
-1. A ์์ฑ์ ํจ์์ ์๋ x๋ฉ์๋๊ฐ `A Prototype Object`์ constructor ์์ฑ๊ฐ์ผ๋ก ๋ค์ด๊ฐ๊ฒ ๋๊ณ , ์ด๋ Aํจ์ ๋ฐ์์ ๋ณ๊ฒฝํ ์ ์๋ค.(A๊ฐ ์์ฑ๋ ๋น์์ ์ ๋ณด๋ง์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ์ฌํ ๋น ๋ถ๊ฐ)
-2. A.prototype.x();๋ฅผ ํ๊ฒ ๋๋ฉด `A Prototype Object`์์ ๊ธฐ๋ณธ ์์ฑ์ธ constructor๊ฐ ์๋ ์๋ก์ด ๋ฉ์๋๋ก ์ถ๊ฐ๋๋ค.
-3. B.x();๋ `A Prototype Object`์ `constructor`์ ์๋ ๋ฉ์๋ x๋ฅผ ์ฐธ์กฐํด์ hello๋ฅผ ์ถ๋ ฅํ๊ณ , B.\__proto__.x();๋ `__proto__`์์ฑ์ผ๋ก `A Prototype Object`์ x๋ฉ์๋๋ฅผ ์ฐธ์กฐํด world๋ฅผ ์ถ๋ ฅํ๋ค.
-
-
-
-
-# โ๏ธClass
-- ํด๋์ค๋ ๋ณ์์ ํจ์ ์ค ์ฐ๊ด์๋ ๋ณ์์ ํจ์๋ฅผ ํ๋๋ก ๋ฌถ์ ๋ ์ฌ์ฉํ๋ ๋ฌธ๋ฒ.
-- ๊ฐ์ฒด ๋จ์๋ก ์ฝ๋ ๊ทธ๋ฃนํ, ์ฝ๋ ์ฌ์ฌ์ฉ์ ์ํด ์ฌ์ฉํ๋ค.
-- ์์ฑ์ ํจํด์ ๋ฉ๋ชจ๋ฆฌ์ฌ์ฉ์ ๋จ์ ์ ๊ทน๋ณตํ๊ธฐ ์ํด์ prototype์ด๋ผ๋ ์์ฑ์ ํค์๋๋ฅผ ์ฌ์ฉํด์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒ์ด JS์์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํ์ค์ ์ธ ๋ฐฉ๋ฒ์ด๋ค.
-- ํํ๋ฒ์ด ๋๋ฌด ๋ฒ๊ฑฐ๋กญ๊ธฐ ๋๋ฌธ์, ES 2015์์๋ ES Classes๋ผ๋ ํ์ค์ผ๋ก Class ํค์๋๋ฅผ ๋ง๋ค์ด์ OOP์ฒ๋ผ ํด๋์ค๋ฅผ ์ข ๋ ์ง๊ด์ ์ผ๋ก ์ฌ์ฉํ ์ ์๊ฒ ํ๋ค.
-
-
-
-- ๊ฐ์ฒด์ ๊ธฐ๋ณธ ์ํ๋ฅผ ์ค์ ํด์ฃผ๋ ์์ฑ์ ๋ฉ์๋ constructor()๋ new์ ์ํด ์๋์ผ๋ก ํธ์ถ๋๋ฏ๋ก, ํน๋ณํ ์ ์ฐจ ์์ด ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ์ ์๋ค.
-- ์ผ๋ฐ์ ์ธ ํจ์๋ ์ ์ธ์์ ์ฌ์ฉํ ๊ฒฝ์ฐ ํธ์ด์คํ
์ด ๋์ง๋ง ํด๋์ค๋ ์ ์ฉ๋์ง ์๋๋ค.
- ์ฆ, ํด๋์ค๋ฅผ ์ ์ธํ ํ์๋ง ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค.
-- Class๋ ์ฌ์ค ํจ์์ด๊ธฐ ๋๋ฌธ์ ์ ์ธ์๊ณผ ํํ์(๋ณ์์ ํ ๋น) ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
-- ํํ์ ์ค ๊ธฐ๋ช
ํด๋์ค ํํ์์์๋ ํด๋์ค ๋ด๋ถ์์๋ง ์ฌ์ฉ๊ฐ๋ฅ.
-- class body {} ์์์๋ strict mode(์๊ฒฉํ ๋ฌธ๋ฒ ์ ์ฉ)
-- ํด๋์ค์ ๋ฉ์๋ ์ฌ์ด์ ์ผํ๋ฅผ ๋ฃ์ง ์๋๋ค. ๊ฐ์ฒด ๋ฆฌํฐ๋ด๊ณผ๋ ๋ค๋ฅด๋ค! ์ผํ ๋ฃ์ผ๋ฉด ๋ฌธ๋ฒ ์๋ฌ ๋ฐ์.
-```javascript
-// ํด๋์ค ์ ์ธ
-class User {
- constructor(name) { this.name = name; }
- sayHi() { alert(this.name); }
-}
-console.log(typeof User); //function
-```
-**< class User {} ๋ฌธ๋ฒ๊ตฌ์กฐ๊ฐ ํ๋ ์ผ >**
-1. User๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ํจ์๋ฅผ ๋ง๋ ๋ค. ํจ์ ๋ณธ๋ฌธ์ ์์ฑ์ ๋ฉ์๋
- constructor์์ ๊ฐ์ ธ์จ๋ค.
- ์์ฑ์ ๋ฉ์๋๊ฐ ์์ผ๋ฉด ๋ณธ๋ฌธ์ด ๋น์์ง ์ฑ๋ก ํจ์๊ฐ ๋ง๋ค์ด์ง๋ค.
-2. sayHi ๊ฐ์ ํด๋์ค ๋ด์์ ์ ์ํ ๋ฉ์๋๋ฅผ User.prototype์ ์ ์ฅํ๋ค.
-```javascript
-// โโโโ
-// User ํจ์๋ฅผ ๋ง๋ค๊ณ ํจ์ ๋ณธ๋ฌธ์ constructor์์ ๊ฐ์ ธ์ด
-constructor(name) {
- this.name = name;
-}
-
-// โโโโ
-//ํด๋์ค ๋ด ๋ฉ์๋๋ User.prototype์ ์ ์ฅ
-sayHi: function
-constructor: User
-```
-
-new User๋ฅผ ํธ์ถํด ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ , ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋ฉ์๋๋ฅผ ํ๋กํ ํ์
์์ ๊ฐ์ ธ์จ๋ค. ์ด ๊ณผ์ ์ด ์๊ธฐ ๋๋ฌธ์ ๊ฐ์ฒด์์ ํด๋์ค ๋ฉ์๋์ ์ ๊ทผ ๊ฐ๋ฅํ๋ค.
-
-์ ๋ฆฌํ์๋ฉด,
-```javascript
-class User {
- constructor(name) { this.name = name; }
- sayHi() { alert(this.name); }
-}
-// ํด๋์ค๋ ํจ์, ์์ฑ์ ๋ฉ์๋์ ๋์ผ
-console.log(User === User.prototype.constructor); //true
-// ํด๋์ค ๋ด๋ถ์์ ์ ์ํ ๋ฉ์๋๋ User.prototype์ ์ ์ฅ
-console.log(User.prototype.sayHi) //alert(this.name);
-// ํ์ฌ ํ๋กํ ํ์
์๋ ๋ฉ์๋๊ฐ ๋ ๊ฐ
-console.log(Object.getOwnPropertyNames(User.prototype));
-// constructor, sayHi
-```
-
-
-
----
-
-## ์ฐธ๊ณ
-class - https://ko.javascript.info/class
-
-prototype
-
- https://medium.com/@bluesh55/javascript-prototype-%EC%9D%B4%ED%95%B4%ED%95%98%EA%B8%B0-f8e67c286b67
-
- http://insanehong.kr/post/javascript-prototype/
\ No newline at end of file