-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_in_depth_12
์๋ฐ์คํฌ๋ฆฝํธ์์๋ new ํค์๋๋ฅผ ์๋ผํ๋๋ผ๋ ์์ฑ์ ํจ์๋ฅผ ์ผ๋ฐ ํจ์์ธ ๊ฒ์ฒ๋ผ ํธ์ถํ๊ณ ์คํํ๋ฏ๋ก ์ค๋ฅ๋ ๋ฐ์ํ์ง ์๊ณ ์ค๋์๋ง ํ๊ฒ ๋๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ ์์ ์คํ๋ผ์ ์์ค๊ฐ ๋ง์์ง๋ฉด ์ค๋ฅ๋ฅผ ์ฐพ์๋ด๋ ๊ฒ์ด ํ๋ค ์๋ ์๋ค. Self-invoking constructor ํจํด์ ์ด๋ฌํ ์ค์๋ฅผ ๊ณ ๋ คํ์ฌ ์์ฑ์๋ฅผ ๊ทธ๋ฅ ํจ์๋ก ํธ์ถ ํ ๋ ์ค์ค๋ก new๋ฅผ ๋ถ์ฌ ๋ค์ ํธ์ถํ๋ ํจํด์ด๋ค.
ECMAScript 6์์ class ํค์๋๊ฐ ๋ณ๋๋ก ๋์ค๋ฉด์ function ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ฒด์งํฅ์ ๊ฐ๋ฐํ๋ ์ํฉ์ ์ ์ ์ค์ด๋ค๊ณ ์์ง๋ง, ์์ง ECMAScript 6์ class ํค์๋๋ฅผ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ๊ฐ 100% ์ง์ํ์ง๋ ์๊ณ ์๋ค.๋ฐ๋ผ์ function ์ผ๋ก ๊ฐ์ฒด์งํฅ์ ๊ตฌํํ ๋ ๋ฐฉ์ด์ ์ผ๋ก ์ด ํจํด์ ์ด์ฉํ์ฌ ์์ฑ์ ํธ์ถ ์ค์๋ฅผ ์ฒ๋ฆฌํ๋ฉด ์ข๋ค. ์ด๋ function ์์ฑ์์ ๊ด๋ จ๋ ํจํด์ผ๋ก ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
// Self-invoking constructor ํจํด์ ๊ตฌํ
(function () {
function Employee(name, manMonth) {
if (!(this instanceof Employee)) {
return new Employee(name, manMonth);
}
this.name = name;
this.manMonth = manMonth;
}
var unikys = Employee("Unikys", 1),
world = new Employee("World", 2);
console.log(unikys);
console.log(world);
}());