-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_in_depth_4
์์์ ๊ตฌํํ๋ ๋ช ๊ฐ์ง ๋ฐฉ๋ฒ์ ์๊ฐํ๋ค.
๊ธฐ์กด์ ์์ ๋ฐฉ์์ ๊ตฌ์กฐ์ ์ธ ๋ถ์์ ์ฑ ์ด์ธ์๋ new ํค์๋ ์์ฒด๊ฐ "์๋ฐ์คํฌ๋ฆฝํธ๋ต์ง ์๋ค"๋ ์๊ฒฌ์ด ๋ง์ด ๋ฐ์๋๋ฉด์
๊ฐ์ฒด๋ฅผ ์์ํ์ฌ ์์ฑํ ์ ์๋ ํจ์๋ฅผ ๋ณ๋๋ก ์ ๊ณตํ๊ฒ ๋์๋ค.
๋จผ์ ๋๊ธ๋ผ์ค ํฌ๋ฝํฌ๋๊ฐ ์ฃผ์ฅํ๋ Object.create ํจ์์ ํํ๋ ๋ค์๊ณผ ๊ฐ๋ค.
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
}Object.create์ ๋ด๋ถ์ ์ธ ํํ๋ ๊ธฐ๋ณธ ํจ์ F๋ฅผ ์ ์ธํ๊ณ F์ ํ๋กํ ํ์
๋ง ์ธ์๋ก ๋ฐ๋ ๊ฐ์ฒด๋ก ์์ ํ์ฌ ์๋ก์ด F ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ๋ฐํํ๋ค. ์ฌ๊ธฐ์ ์ฃผ์ํ ์ ์ Object.create ํจ์์ ์ธ์๋ ์์ฑ์์ธ ํจ์๊ฐ ์๋๋ผ ํ๋กํ ํ์
์ผ๋ก ์ค์ ํ ๊ฐ์ฒด ๋๋ ์ธ์คํด์ค๋ผ๋ ์ ์ด๋ค.
Object.createํจ์๋ฅผ ํตํด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด, ๊ฐ๋ฐ์๊ฐ new ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ํจ์ ํธ์ถ๋ก ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. new ํค์๋๋ฅผ ์ฌ์ฉํ ๋์๋ ๋ฌ๋ฆฌ ์ ์ฒด์ ์ผ๋ก ์์ค์ ์์ฑ์์ ๊ฐ๋
์ด ์ฝํด์ง๊ณ ๊ฐ์ฒด์ ์ธ์คํด์ค์ ์ธ์คํด์ค ๊ฐ์ ์์์ ๊ฐ์กฐํ๋ ๊ฒ์ด Object.create ํจ์์ ํน์ง์ด๋ผ๊ณ ๋ณผ ์ ์๋ค.
var person = {
yell: function() {
console.log("My name is " + this.name);
}
};
var unikys = Object.create(person);
unikys.name = "Unikys";
unikys.yell();
// Object.create ํจ์๋ก ์์ฑํ ๊ฐ์ฒด์ ์์ ํ์ธ์ด ์ด๋ ค์ด ์
//console.log(unikys instanceof person); // TypeError!
// Object.create ํจ์๋ก ์์ฑํ ๊ฐ์ฒด์ ํ๋กํ ํ์
ํ์ธ
console.log(Object.getPrototypeOf(unikys) === person); // true
console.log(Object.getPrototypeOf(person)); // Object {}
// Object.create ํจ์์ ์ธ์๋ก ์ผ๋ฐ ๊ฐ์ฒด๋ฅผ ํ์ฉํ ๊ฒฝ์ฐ์ ์์ ํ์ธ
console.log(person.isPrototypeOf(unikys)); // true
console.log(Object.prototype.isPrototypeOf(unikys)); // true๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ new ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์์ฑ์ ์์์ ์์ฑ๋๋ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ ์์
์ ์ํํ ์ ์๋๋ฐ, Object.create ํจ์์ ๊ตฌํ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๋ฉด Object.create์ ๋ณ๋๋ก ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๊ณ ์๋๊ฒ์ ๋ณผ ์ ์๋ค. ์ด๋ฌํ ๊ณผ์ ์ ํ๋์ ํจ์๋ก ๋ฌถ์ด์ ๊ฐ์ฒด๋ณ๋ก ๊ฐ๋จํ ์์ ํจ์๋ฅผ ๋ง๋ค ์๋ ์์ง๋ง, ํ์ค์ Object.create ํจ์์ 2๋ฒ์งธ ์ธ์๋ฅผ ์ ํ์ ์ผ๋ก ๋ฐ์์ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋๋ก ๋์ด ์๋ค.
// Object.create ํจ์๋ก ์์ ๊ฐ๋ฅํ ์์ฑ ์ด๊ธฐํ ์
var unikys = Object.create(Person.prototype, {
name : {
value: "Unikys",
configurable: true,
enumerable: true,
writable: true
}
});function Person() {
this.name = "anonymous";
}
function Unikys() {
this.name = "Unikys";
}
Unikys.prototype = Object.create(Person.prototype, {
constructor: {
value: Unikys
}
});
var unikys = new Unikys();
console.log(unikys instanceof Unikys); // true
console.log(unikys instanceof Person); // true
console.log(unikys.constructor); // function Unikys()์ด๋ ๊ฒ Object.create์ 2๋ฒ์งธ ์ธ์๋ก Unikys.prototype์ ์์ฑ์ ์์ฑ์ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ์ค์ ํ์ฌ ๊ธฐ์กด์ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์์ฒด์ ์ผ๋ก ๊ฐ์ง๊ณ ์์๋, ์์ฑ์์ ์ฐ๊ฒฐ์ด ๊นจ์ง๋ ํ์์ ๋ณด์ํ์ฌ new ํค์๋๋ฅผ ํตํ ์์์ ์ฌ์ฉํ ์ ์๋ค.
ECMAScript 6์์๋ ์๋ก์ด ํค์๋์ธ class์ extends๋ฅผ ์ ์ํ์๋ค. ๊ธฐ์กด์ new ํค์๋๋ฅผ ํจ์์ ํจ๊ป ์ฌ์ฉํ ๋๋ ๊ฐ๋ฐ์์ ์ค์๋ก ์ธํ ์ค๋์์ ์ผ์ด๋๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์์ ์ค๋์์ ์์ธ์ ์ฐพ์๋ด๊ธฐ๊ฐ ์ด๋ ค์ ๋ค. ๊ทธ๋์ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ฒ๋ผ new ํค์๋์ ์์ฑ์์ ํจ์ ํธ์ถ ๊ฐ์ฒด๋ฅผ ๊ตฌ๋ถํ์ฌ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋๋ก ๋ณด์ํ์๋ค.
class Person {
constructor() {
this.name = "anonymous";
}
}
class Unikys extends Person {
constructor() {
super();
this.name = "Unikys";
}
}
var unikys = new Unikys();
console.log(unikys instanceof Unikys); // true
console.log(unikys instanceof Person); // true
console.log(unikys.constructor); // class Unikys extend PersonES6 Class๋ ๋จ์ง prototype ์์์ ๋ฌธ๋ฒ์คํ์ผ ๋ฟ์ธ๊ฐ?
// ํฌ๋ก์ค ๋ธ๋ผ์ฐ์ง์ ์ํ Object.create ํจ์ ์ ์
(function () {
if (!Object.create) {
Object.create = (function() {
function F() {}
return function (o) {
F.prototype = o;
return new F();
}
}());
}
}());