-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_jquery_basic_3_3a
ํค์๋
Prototype-
__proto__(ECMAScript ๋ช ์ธ๋[[prototype]]๋ก ํ๊ธฐ) -
Constructor(ECMAScript ๋ช ์ธ๋[[construct]]๋ก ํ๊ธฐ)
๋ด๋ถ ๊ตฌํ
- ๋ชจ๋ ๊ฐ์ฒด๋
__proto__๊ฐ์ฒด๋ฅผ ํ๋กํผํฐ๋ก ๊ฐ๋๋ค. (๋ด๋ถ ์ฐธ์กฐ์ฉ์ผ๋ก ์ฌ์ฉ) - ์์ฑ์(๋๋ ์์ฑ์ ํจ์)๋
prototype๊ฐ์ฒด๋ฅผ ํ๋กํผํฐ๋ก ๊ฐ๋๋ค. -
prototype๊ฐ์ฒด๋constructor๊ฐ์ฒด๋ฅผ ํ๋กํผํฐ๋ก ๊ฐ๋๋ค.
// ์์ฑ์ ์ ์ธ
function Person (name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
// Person์ ์ด๋ป๊ฒ toString()์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ ๊ฒ์ผ๊น?
function Person (name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
// toString์ Person์ ์์ฑ์ด ์๋๋ค.
console.log(Person.toString());Function.toString() ์์
Function.prototype (MDN)

// ์ธ์คํด์ค ์์ฑ
function Person (name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var person1 = new Person('Lee');
console.log(person1.getName());new๋ฅผ ์ด์ฉํด ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ฒ ๋๋ฉด ์๋ ์ฝ๋๊ฐ ๋ด๋ถ์์ ๋์ํ๋ค.
var person1 = new Person('Lee');
// ์์ง ๋ด๋ถ์์ ํ๋ ์ผ
person1 = {}; // ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ
Person.call(person1, 'myName'); // call ์ด์ฉํด Person ์์ฑ์๋ฅผ ํธ์ถ(this๋ฅผ person1๋ก)
person1.__proto__ = Person.prototype; // ํ๋กํ ํ์
์ ์ฐ๊ฒฐํ๋ค.
person1.getName(); // 'Lee'๊ฐ์ฒด์์ฑ ๋ฐ ํ๋กํ ํ์ ์ฐ๊ฒฐ
์ ์ฝ๋๊ฐ ์คํ๋๋ฉด ๋ค์๊ณผ ๊ฐ์ ํํ๋ก ํ๋กํ ํ์ ์ฒด์ธ์ด ์์ฑ๋๋ค.

ํ๋กํ ํ์ ์ฒด์ธ์ ์ํ ๊ตฌ์กฐ
์๋ฐ์คํฌ๋ฆฝํธ์ ๋ชจ๋ ๊ฐ์ฒด๋ ์์ ์ ๋ถ๋ชจ์ธ prototype ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋งํฌ ํํ์ ์จ๊ฒจ์ง ํ๋กํผํฐ๊ฐ ์๋ค. ECMAScript์์๋ ์ด ๋งํฌ๋ฅผ ์๋ฌต์ ๋งํฌ(implicit link)๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ ์ด ๋งํฌ๋ ๋ชจ๋ ๊ฐ์ฒด์ __proto__ ํ๋กํผํฐ์ ์ ์ฅ๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์ ์์ฑ ๊ท์น์ ๋ชจ๋ ๊ฐ์ฒด๋ ์์ ์ ์์ฑํ ์์ฑ์ ํจ์์ prototype ํ๋กํผํฐ๊ฐ ๊ฐ๋ฆฌํค๋ prototype ๊ฐ์ฒด๋ฅผ ์์ ์ __proto__ ํ๋กํผํฐ๋ก ์ฐ๊ฒฐํ๋ค.
ํ๋กํ ํ์
์ฒด์ด๋์ด๋ ํน์ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ ๋ฉ์๋์ ์ ๊ทผํ๋ ค๊ณ ํ ๋, ํด๋น ๊ฐ์ฒด์ ์ ๊ทผํ๋ ค๋ ํ๋กํผํฐ๋ ๋ฉ์๋๊ฐ ์๋ค๋ฉด __proto__ ๋งํฌ๋ฅผ ๋ฐ๋ผ ์์ ์ ๋ถ๋ชจ ์ญํ ์ ํ๋ prototype ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ฐจ๋ก๋๋ก ๊ฒ์ํ๋ ๊ฒ์ ๋งํ๋ค.
ํ๋กํ ํ์
์ฒด์ด๋์ ํตํด ์์ ์ ํ๋กํผํฐ๋ฟ๋ง ์๋๋ผ, ์์ ์ ๋ถ๋ชจ์ญํ ์ ํ๋ prototype ๊ฐ์ฒด์ ํ๋กํผํฐ์๋ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
var a = {
attr1: 'a'
};
var b = {
__proto__: a,
attr2: 'b'
};
var c = {
__proto__: b,
attr3: 'c'
};
c.attr1 // 'a'a์ ์กด์ฌํ๋ attr1 ์์ฑ์ ๊ฒ์ํ๋ ๊ณผ์
- c๊ฐ์ฒด ๋ด๋ถ์ attr1 ์์ฑ์ ์ฐพ๋๋ค. -> ์๋ค.
- c๊ฐ์ฒด์
__proto__์์ฑ์ด ์กด์ฌํ๋์ง ํ์ธํ๋ค. -> ์๋ค. - c๊ฐ์ฒด์
__proto__์์ฑ์ด ์ฐธ์กฐํ๋ ๊ฐ์ฒด๋ก ์ด๋ํ๋ค. -> b๊ฐ์ฒด๋ก ์ด๋ - b๊ฐ์ฒด ๋ด๋ถ์ attr1 ์์ฑ์ ์ฐพ๋๋ค. -> ์๋ค.
- b๊ฐ์ฒด์
__proto__์์ฑ์ด ์กด์ฌํ๋์ง ํ์ธํ๋ค. -> ์๋ค. - b๊ฐ์ฒด์
__proto__์์ฑ์ด ์ฐธ์กฐํ๋ ๊ฐ์ฒด๋ก ์ด๋ํ๋ค. -> a๊ฐ์ฒด๋ก ์ด๋ - a๊ฐ์ฒด ๋ด๋ถ์ attr1 ์์ฑ์ ์ฐพ๋๋ค. -> ์๋ค. ์ฐพ์ ์์ฑ์ ๊ฐ์ ๋ฆฌํดํ๋ค.
์ด๋ค ๊ฐ์ฒด์๋ ์กด์ฌํ์ง ์๋ attr0 ์์ฑ์ ๊ฒ์ํ๋ ๊ณผ์
- a๊ฐ์ฒด ๋ด๋ถ์ attr0 ์์ฑ์ ์ฐพ๋๋ค. -> ์๋ค.
- a๊ฐ์ฒด์
__proto__์์ฑ์ด ์กด์ฌํ๋์ง ํ์ธํ๋ค. -> ์๋ค. - a๊ฐ์ฒด์
__proto__์์ฑ์ด ์ฐธ์กฐํ๋ ๊ฐ์ฒด๋ก ์ด๋ํ๋ค. ->Object.prototype๋ก ์ด๋ -
Object.prototype์์ attr0 ์์ฑ์ ์ฐพ๋๋ค. -> ์๋ค. -
Object.prototype์์__proto__์์ฑ์ ์ฐพ๋๋ค. -> ์๋ค.
๋ชจ๋ ํ๋กํ ํ์
์ฒด์ธ์ ๋์ ํญ์ Object.prototype ์ด๋ค.
๊ทธ๋์ Object.prototype์ __proto__ ์์ฑ์ด ์๋ค.
attr0 ๋ผ๋ ์์ฑ์ ํ๋กํ ํ์
์ ๋ง์ง๋ง ๋จ๊ณ์ธ Object.prototype์
์กด์ฌํ์ง ์๊ณ Object.prototype์๋ __proto__ ์์ฑ์ด ์กด์ฌํ์ง ์์ผ๋ ํ์์ ์ข
๋ฃํ๊ณ undefined๋ฅผ ๋ฆฌํดํ๋ค.
ํ๋กํ ํ์
๊ธฐ๋ฐ์ ๊ฐ์ฒด์งํฅ ์์์ ๊ตฌํํ ์ ์๋ค.
ํ๋กํ ํ์
๊ธฐ๋ฐ์ ์์ ๊ตฌํ
ํ๋กํ ํ์
๊ธฐ๋ฐ์ ์์ ๊ตฌํ2
ES6์์ class์ extends๋ฅผ ์ด์ฉํ ์์ ๊ตฌํ
์กฐ๊ธ ๋ ์ฐ์ํ๊ฒ ์์์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ
- ํ๋กํ ํ์ ์์์ ๊ตฌํํ๋ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ๋ฒ
- ES6์์ class๋ฅผ ์ด์ฉํ ์์ ๋ฐฉ๋ฒ
์ฝ๊ฒ ์ดํดํ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํ๋กํ ํ์ ์ฒด์ธ