|
| 1 | +์๋ฐ์คํฌ๋ฆฝํธ๋ ํด๋์ค ๊ธฐ๋ฐ์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๊ฐ ์๋์ง๋ง ํ๋กํ ํ์
๊ธฐ๋ฐ์ ์์ฃผ ์ ์ฐํ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ ์คํ์ผ๋ก ๊ฐ์ฒด ์งํฅ ์ธ์ด์ ์์๊ณผ ์บก์ํ ๋ฑ์ ์ถ์์ ๊ฐ๋
๋ ๊ตฌํํ ์ ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ์ด ์ฅ์์ ES6์ ๋ฑ์ฅํ ํด๋์ค(Class)์ ๋ํด ์์๋ด
๋๋ค. |
| 2 | + |
| 3 | +## ํด๋์ค์ ์์ฑ์ ํจ์ |
| 4 | +> ES6์ ํด๋์ค๊ฐ ํ๋กํ ํ์
๊ธฐ๋ฐ ๊ฐ์ฒด ์งํฅ ๋ชจ๋ธ์ ํ์งํ ๊ฒ์ด ์๋๋๋ค. ํด๋์ค๋ ํจ์์ด๊ธฐ ๋๋ฌธ์ด์ง์. |
| 5 | +
|
| 6 | +์ฐ๋ฆฌ๋ ๊ธฐ์กด ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๊ตฌํํ ๋, ์์ฑ์ ํจ์์ ํ๋กํ ํ์
, ํด๋ก์ ๋ฅผ ์ด์ฉํด์์ต๋๋ค. |
| 7 | +```js |
| 8 | +var Person = (function () { |
| 9 | + // Constructor, ์์ฑ์ ํจ์ |
| 10 | + function Person(name) { |
| 11 | + this._name = name; |
| 12 | + } |
| 13 | + |
| 14 | + // public method |
| 15 | + Person.prototype.sayHi = function () { |
| 16 | + console.log('HELLO! ' + this._name); |
| 17 | + }; |
| 18 | + |
| 19 | + // return constructor |
| 20 | + return Person; |
| 21 | +}()); |
| 22 | + |
| 23 | +// ์ธ์คํด์ค ์์ฑ |
| 24 | +var me = new Person('Amy'); |
| 25 | +me.sayHi(); // HELLO! Amy |
| 26 | + |
| 27 | +console.log(me instanceof Person); // true |
| 28 | +``` |
| 29 | + |
| 30 | +์ด๋ฅผ ํด๋์ค๋ก ๊ตฌํํด๋ณผ๊น์? |
| 31 | +```js |
| 32 | +class Person { |
| 33 | + // constructor, ์์ฑ์ |
| 34 | + constructor(name) { |
| 35 | + this._name = name; |
| 36 | + } |
| 37 | + |
| 38 | + sayHi() { |
| 39 | + console.log(`HELLO! ${this._name}`); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// ์ธ์คํด์ค ์์ฑ |
| 44 | +const me = new Person('Amy'); |
| 45 | +me.sayHi(); // HELLO! Amy |
| 46 | + |
| 47 | +console.log(me instanceof Person); // true |
| 48 | +``` |
| 49 | + |
| 50 | +๋์ผํ ๋์์ ํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด์ฃ ? ๊ตฌ๋ฌธ๋ ๋จ์ํ๋์์ต๋๋ค. ์ฆ, ES6์ ํด๋์ค๋ ํจ์์ด๋ฉฐ ๊ธฐ์กด ํ๋กํ์
๊ธฐ๋ฐ ํจํด์ **๋ฌธ๋ฒ์ ์คํ(Syntactic sugar)** ์ด ๋ฉ๋๋ค. ๋จ, ํด๋์ค์ ์์ฑ์ ํจ์๊ฐ ๋์ผํ๊ฒ ๋์ํ์ง๋ ์์ผ๋๊น ์ฃผ์(์ผ๊ฐ์์ ์ด๋ฐ ์ด์ ๋ก ํด๋์ค๋ฅผ ๋ฌธ๋ฒ์ ์คํ์ผ๋ก ์ธ์ ํ์ง ์์์)ํ์ธ์. |
| 51 | + |
| 52 | +<br> |
| 53 | + |
| 54 | +## ํด๋์ค ์ ์ |
| 55 | +> ํด๋์ค๋ class ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ํ๋ฉฐ, ํ์ค์นผ ์ผ์ด์ค๋ก ์์ฑํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์
๋๋ค. |
| 56 | +
|
| 57 | +ํด๋์ค๋ฅผ ์ด๋ป๊ฒ ์์ฑํ๋์ง๋ ์ธ๊ธํ์ง ์๊ฒ ์ต๋๋ค. ๊ธฐ์กด ํ๋กํ ํ์
๊ณผ ์ ์ฌํ๊ธฐ ๋๋ฌธ์ด์ฃ . ์ด ์ฅ์์๋ ํด๋์ค์ ํน์ง๋ง ์ง๊ณ ๋์ด๊ฐ๋๋ค. |
| 58 | + |
| 59 | +1. ํด๋์ค๋ ํด๋์ค ์ ์ธ๋ฌธ ์ด์ ์ ์ฐธ์กฐํ ์ ์์ต๋๋ค. |
| 60 | + - ์ด๋ let, const์ ๊ฐ์ต๋๋ค. ์ฆ, ํธ์ด์คํ
๋๋ let, const ๋ณ์์ฒ๋ผ ์ ์ธ๋ฌธ ์ด์ ์ ์ผ์์ ์ฌ๊ฐ์ง๋(TDZ)์ ๋น ์ง๊ธฐ ๋๋ฌธ์ด์ฃ . |
| 61 | + - ํธ์ด์คํ
์ var, let, const, function, function*, class ํค์๋๋ฅผ ์ฌ์ฉํ ๋ชจ๋ ์ ์ธ๋ฌธ์ ์ ์ฉ๋ฉ๋๋ค. |
| 62 | + |
| 63 | +<br> |
| 64 | + |
| 65 | +2. ํํ์์ผ๋ก๋ ํด๋์ค๋ฅผ ์ ์ํ ์ ์์ผ๋ฉฐ ์ต๋ช
์ผ ์ ์์ต๋๋ค. |
| 66 | + ```js |
| 67 | + const NamedClass = class UnNamedClass {}; |
| 68 | + |
| 69 | + const name = new NamedClass(); |
| 70 | + console.log(name); // UnNamedClass {} |
| 71 | + |
| 72 | + new UnNamedClass(); // ReferenceError: UnNamedClass is not defined |
| 73 | + ``` |
| 74 | + - ํด๋์ค๊ฐ ํ ๋น๋ ๋ณ์๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๊ธฐ๋ช
ํด๋์ค๋ก ์์ฑํ๋ค๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค. ๊ทธ ์ด์ ๋ **ํด๋์ค ํํ์์ ์ฌ์ฉํ ์ด๋ฆ์ ์ธ๋ถ ์ฝ๋์์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ด์ฃ **. |
| 75 | + |
| 76 | +<br> |
| 77 | + |
| 78 | +3. ์์ฑ์ ํจ์์ ๋ง์ฐฌ๊ฐ์ง๋ก new ์ฐ์ฐ์์ ํจ๊ป ํด๋์ค๋ฅผ ํธ์ถํ๋ฉด ํด๋์ค์ ์ธ์คํด์ค๊ฐ ์์ฑ๋ฉ๋๋ค. |
| 79 | + ```js |
| 80 | + class NamedClass {}; |
| 81 | + const name = NamedClass(); // TypeError: Class constructor NamedClass cannot be invoked without 'new' |
| 82 | + ``` |
| 83 | + - ํด๋์ค๋ฅผ new ์ฐ์ฐ์ ์์ด ํธ์ถํ๋ฉด ํ์
์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค. |
| 84 | + |
| 85 | +<br> |
| 86 | + |
| 87 | +4. ํด๋์ค๋ ํด๋์ค ๋ด๋ถ์ ์บก์ํ๋ ๋ณ์, **ํด๋์ค ํ๋(Class Field)** ๋ฅผ ๊ฐ์ง๋๋ค. |
| 88 | + - ๋ฐ์ดํฐ ๋ฉค๋ฒ, ๋ฉค๋ฒ ๋ณ์๋ผ๊ณ ๋ ๋ถ๋ฅด๋ฉฐ ์ธ์คํด์ค์ ํ๋กํผํฐ ํน์ ์ ์ ํ๋กํผํฐ๊ฐ ๋ ์ ์์ต๋๋ค. |
| 89 | + - this์ ์ถ๊ฐํ ํ๋กํผํฐ๋ฅผ ํด๋์ค ๊ธฐ๋ฐ ๊ฐ์ฒด์งํฅ ์ธ์ด์์ ํด๋์ค ํ๋๋ผ๊ณ ์ง์นญํ์ฃ . |
| 90 | + |
| 91 | +<br> |
| 92 | + |
| 93 | +5. constructor๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ํด๋์ค ํ๋๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํ ๋ฉ์๋์
๋๋ค. |
| 94 | + ```js |
| 95 | + class NamedClass {}; |
| 96 | + const name = NamedClass(); // TypeError: Class constructor NamedClass cannot be invoked without 'new' |
| 97 | + // NamedClass๋ ์์ฑ์ ํจ์(constructor)์
๋๋ค. |
| 98 | + console.log(Object.getPrototypeOf(name).constructor === NamedClass); // true |
| 99 | + ``` |
| 100 | + - new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถ๋๋ ๊ฒ์ด constructor๋ก ํ๋ผ๋ฏธํฐ์ ์ ๋ฌํ ๊ฐ์ ํด๋์ค ํ๋์ ํ ๋นํ ์ ์์ต๋๋ค. |
| 101 | + - constructor๋ ์ธ์คํด์ค์ ์์ฑ๊ณผ ๋์์ ํด๋์ค ํ๋์ ์์ฑ๊ณผ ์ด๊ธฐํ๋ฅผ ์คํํฉ๋๋ค. |
| 102 | + - constructor๋ ํด๋์ค ๋ด์ ํ ๊ฐ๋ง ์กด์ฌํ๋ฉฐ 2๊ฐ ์ด์์ผ ๊ฒฝ์ฐ ๋ฌธ๋ฒ ์๋ฌ(SyntaxError)๊ฐ ๋ฐ์ํฉ๋๋ค. |
| 103 | + - constructor๋ฅผ ์๋ตํ๋ฉด ํด๋์ค์ constructor() {}, ์ฆ ๋น ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ ๊ฒ์ฒ๋ผ ๋์ํฉ๋๋ค. |
| 104 | + |
| 105 | +<br> |
| 106 | + |
| 107 | +6. ํด๋์ค ํ๋์๋ ๋ฉ์๋๋ง ์ ์ธํ ์ ์์ต๋๋ค. |
| 108 | + ```js |
| 109 | + class NamedClass { |
| 110 | + name = ''; // SyntaxError |
| 111 | +
|
| 112 | + // ํด๋์ค ํ๋์ ์ ์ธ๊ณผ ์ด๊ธฐํ๋ ๋ฐ๋์ constructor์์ ์ค์ํฉ๋๋ค. |
| 113 | + constructor(name) { |
| 114 | + this.name = name; |
| 115 | + } |
| 116 | + } |
| 117 | + ``` |
| 118 | + |
| 119 | + > ํ์ฌ๋ ์ ์์ ์ผ๋ก ๋์ํ๋๋ฐ, TC39 ํ๋ก์ธ์ค์ stage 3(candidate) ๋จ๊ณ์ ์๋ ํด๋์ค ๋ชธ์ฒด์์ ์ง์ ์ธ์คํด์ค ํ๋๋ฅผ ์ ์ธํ๊ณ private ์ธ์คํด์ค ํ๋๋ฅผ ์ ์ธํ ์ ์๋ ํ๋กํฌ์ (Class field declarations proposal)์ ํ๋ ์ ์๋ฅผ ์ต์ ๋ธ๋ผ์ฐ์ ์ ์ต์ Node.js๊ฐ ๊ตฌํํ์๊ธฐ ๋๋ฌธ์
๋๋ค. |
| 120 | + |
| 121 | + <br> |
| 122 | + |
| 123 | + - constructor ๋ด๋ถ์์ ์ ์ธํ ํด๋์ค ํ๋๋ ํด๋์ค๊ฐ ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ this์ ๋ฐ์ธ๋ฉ๋๋ฉฐ, ํด๋์ค ์ธ์คํด์ค๋ฅผ ํตํด ์ธ๋ถ์์ ์ง์ ์ฐธ์กฐํ ์ ์์ต๋๋ค. publicํ ๋ฉค๋ฒ๋ก์ ์ทจ๊ธ๋๋ ๊ฒ์ด์ฃ . |
| 124 | + |
| 125 | + <br> |
| 126 | + |
| 127 | + > ์ต์ ๋ธ๋ผ์ฐ์ ์ Node.js 12๋ฒ์ ์ด์์์๋ ์ฌ๋ฌ ์์ฑ๋ค์ ์ง์ํ๊ณ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ [์ฌ๊ธฐ](https://github.com/tc39/proposals/blob/master/README.md)๋ฅผ ์ฐธ์กฐํด์ฃผ์ธ์. |
| 128 | + |
| 129 | +<br> |
| 130 | + |
| 131 | +7. getter/setter๋ก ํด๋์ค ํ๋์ ๊ฐ์ ์ฐธ์กฐํ๊ฑฐ๋, ์กฐ์ํ ์ ์์ต๋๋ค. |
| 132 | + - ์ฐ๋ฆฌ๊ฐ ์๊ณ ์๋ ๊ทธ ์ ๊ทผ์ ํ๋กํผํฐ๊ฐ ๋ง์ต๋๋ค. |
| 133 | + |
| 134 | +<br> |
| 135 | + |
| 136 | +8. ์ ์ (static) ๋ฉ์๋๋ฅผ ์ ์ํ ์ ์์ผ๋ฉฐ, ํด๋์ค ์ธ์คํด์ค๊ฐ ์๋๋ผ ํด๋์ค ์ด๋ฆ์ผ๋ก ํธ์ถํ ์ ์์ต๋๋ค. |
| 137 | + - ํด๋์ค ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์์๋ ๋ฉ๋๋ค. ์ด๋ ์ ์ ๋ฉ์๋์ this๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ ๊ฒ์
๋๋ค. |
| 138 | + ```js |
| 139 | + class NamedClass { |
| 140 | + constructor(prop) { |
| 141 | + this.prop = prop; |
| 142 | + } |
| 143 | +
|
| 144 | + static staticMethod() { |
| 145 | + // ์ ์ ๋ฉ์๋๋ this๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. |
| 146 | + // ์ ์ ๋ฉ์๋ ๋ด๋ถ์์ this๋ ํด๋์ค์ ์ธ์คํด์ค๊ฐ ์๋ ํด๋์ค ์์ ์ ๊ฐ๋ฆฌํต๋๋ค. |
| 147 | + return 'staticMethod'; |
| 148 | + } |
| 149 | +
|
| 150 | + prototypeMethod() { |
| 151 | + return this.prop; |
| 152 | + } |
| 153 | + } |
| 154 | +
|
| 155 | + // ์ ์ ๋ฉ์๋๋ ํด๋์ค ์ด๋ฆ์ผ๋ก ํธ์ถํฉ๋๋ค. |
| 156 | + console.log(NamedClass.staticMethod()); |
| 157 | +
|
| 158 | + const name = new NamedClass(123); |
| 159 | + // ์ ์ ๋ฉ์๋๋ ์ธ์คํด์ค๋ก ํธ์ถํ ์ ์์ต๋๋ค. |
| 160 | + console.log(name.staticMethod()); // Uncaught TypeError: NamedClass.staticMethod is not a function |
| 161 | + ``` |
| 162 | + - ์ด์ ๋ ๊ธฐ์กด ํ๋กํ ํ์
๊ธฐ๋ฐ์ ๋ฌธ๋ฒ์ ์คํ์ด ํด๋์ค์ด๊ธฐ ๋๋ฌธ์
๋๋ค. ์ ์์ ๋ฅผ ES5๋ก ํํํด ๋ณผ๊น์? |
| 163 | + ```js |
| 164 | + var NamedClass = (function () { |
| 165 | + // ์์ฑ์ ํจ์ |
| 166 | + function NamedClass(prop) { |
| 167 | + this.prop = prop; |
| 168 | + } |
| 169 | +
|
| 170 | + NamedClass.staticMethod = function () { |
| 171 | + return 'staticMethod'; |
| 172 | + }; |
| 173 | +
|
| 174 | + NamedClass.prototype.prototypeMethod = function () { |
| 175 | + return this.prop; |
| 176 | + }; |
| 177 | +
|
| 178 | + return NamedClass; |
| 179 | + }()); |
| 180 | +
|
| 181 | + var name = new NamedClass(123); |
| 182 | + console.log(name.prototypeMethod()); // 123 |
| 183 | + console.log(NamedClass.staticMethod()); // staticMethod |
| 184 | + console.log(name.staticMethod()); // Uncaught TypeError: name.staticMethod is not a function |
| 185 | + ``` |
| 186 | + - prototype ํ๋กํผํฐ๋ ํจ์ ๊ฐ์ฒด๊ฐ ์์ฑ์๋ก ์ฌ์ฉ๋ ๋, ์ด ํจ์๋ฅผ ํตํด ์์ฑ๋ ๊ฐ์ฒด์ ๋ถ๋ชจ ์ญํ ์ ํ๋ ํ๋กํ ํ์
๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ฏ๋ก **์ ์ ๋ฉ์๋์ธ staticMethod๋ ์์ฑ์ ํจ์ NamedClass์ ๋ฉ์๋(ํจ์๋ ๊ฐ์ฒด์ด๋ฏ๋ก ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ ์์ด์)์ด๊ณ , ์ผ๋ฐ ๋ฉ์๋์ธ prototypeMethod๋ ํ๋กํ ํ์
๊ฐ์ฒด NamedClass.prototype์ ๋ฉ์๋์
๋๋ค. ๋ฐ๋ผ์ staticMethod๋ name์์ ํธ์ถํ ์ ์๋ ๊ฒ์ด์ฃ **! |
| 187 | + |
| 188 | +<br> |
| 189 | + |
| 190 | +9. extends ํค์๋๋ก ํด๋์ค๋ฅผ ์์(Class Inheritance) ํ ์ ์์ต๋๋ค. |
| 191 | + - ์ค๋ฒ๋ผ์ด๋ฉ(Overriding), ์ค๋ฒ๋ก๋ฉ(Overloading)์ด ํ๋กํ ํ์
์ฒด์ธ์ ์ํด ๊ตฌํ๋ ์ ์์ต๋๋ค. |
| 192 | + - super() ํค์๋๋ก ๋ถ๋ชจ ํด๋์ค๋ฅผ ์ฐธ์กฐ(Reference)ํ๊ฑฐ๋ ๋ถ๋ชจ ํด๋์ค์ constructor๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. |
| 193 | + - ์์ ํด๋์ค์์ constructor๋ฅผ ์ ์ธํ์ง ์์ผ๋ฉด ๋ถ๋ชจ์ constructor๋ฅผ ๋ฐ๋ผ๋ด
๋๋ค. |
| 194 | + - ๊ทธ๋ฌ๋ ์์ ํด๋์ค์์ constructor๋ฅผ ์ ์ธํ์์๋ super() ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด ์ฐธ์กฐ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค. |
| 195 | + |
| 196 | +<hr> |
| 197 | +<br> |
0 commit comments