-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_jquery_basic_3_3a
ํค์๋
Prototype-
__proto__==[[prototype]] Constructor
ECMAScript ๊ท๊ฒฉ ๋ด์ฉ
- ๋ชจ๋ ๊ฐ์ฒด๋
__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'๊ฐ์ฒด์์ฑ ๋ฐ ํ๋กํ ํ์ ์ฐ๊ฒฐ
์ ์ฝ๋๊ฐ ์คํ๋๋ฉด ๋ค์๊ณผ ๊ฐ์ ํํ๋ก ํ๋กํ ํ์ ์ฒด์ธ์ด ์์ฑ๋๋ค.

ํ๋กํ ํ์ ์ฒด์ธ์ ํตํด ๊ฐ์ฒด์ ๋ฉ์๋๋ ์์ฑ์ ์ฐพ์๊ฐ๋ ๊ฒ
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๋ฅผ ๋ฆฌํดํ๋ค.
์ฝ๊ฒ ์ดํดํ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํ๋กํ ํ์ ์ฒด์ธ