๐ย Reference
๐ย Chapter
Class
Object.create()
Prototype
- Prototype
- ํ๋กํ ํ์
- ์ ์ ์
- ํ๋กํ ํ์
์์ฑ
- ํํ 'ํ๋กํ ํ์
'์ด๋ผ๊ณ ๋ถ๋ฅด๋ ๊ฒ์ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ์กฐ์์ผ๋ก๋ถํฐ ๋ฌผ๋ ค๋ฐ์ ์ฐธ์กฐ ๊ฐ์ ์๋ฏธํ๋ค.
- ํจ์ ์
์ฅ: "๋ด๊ฐ ๋ง๋ ์์๋ค์๊ฒ ์ด ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๊ฒ ํ ๊ฑฐ์ผ"๋ผ๋ ์๋ฏธ์
prototype ์์ฑ์ ๊ฐ์ง๋ค.
- ๊ฐ์ฒด ์
์ฅ: "๋๋ฅผ ๋ง๋ ๋ถ๋ชจ์ ๋ ์ํผ๊ฐ ์ด๋ ์์ง?"๋ฅผ ๊ฐ๋ฆฌํค๋ ์จ๊ฒจ์ง ์ ๋ณด์ด๋ค.
- ์์์ ๊ตฌ์ฒด์ ์ธ ์๋จ์ด๋ค.
- Prototype Link, Prototype Object ๋ผ๋ ๊ฒ์ด ์กด์ฌํ๋ค.
function Person() {
this.eyes = 2;
this.nose = 1;
}
var kim = new Person();
var park = new Person();
console.log(kim.eyes); // => 2
console.log(kim.nose); // => 1
console.log(park.eyes); // => 2
console.log(park.nose); // => 1
- kim๊ณผ park์ eyes์ nose๋ฅผ ๊ณตํต์ ์ผ๋ก ๊ฐ์ง๊ณ ์๋๋ฐ, ๋ฉ๋ชจ๋ฆฌ์๋ eyes์ nose๊ฐ ๋ ๊ฐ์ฉ ์ด 4๊ฐ ํ ๋น๋๋ค.
- ๊ฐ์ฒด๋ฅผ 100๊ฐ ๋ง๋ค๋ฉด 200๊ฐ์ ๋ณ์๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น๋๋ค.
- ๋ฐ๋ก ์ด๋ฐ ๋ฌธ์ ๋ฅผ ํ๋กํ ํ์
์ผ๋ก ํด๊ฒฐํ ์ ์์ต๋๋ค.
function Person() {}
Person.prototype.eyes = 2;
Person.prototype.nose = 1;
var kim = new Person();
var park = new Person():
console.log(kim.eyes); // => 2
- Person.prototype์ด๋ผ๋ ๋น Object๊ฐ ์ด๋๊ฐ์ ์กด์ฌํ๊ณ , Person ํจ์๋ก๋ถํฐ ์์ฑ๋ ๊ฐ์ฒด(kim, park)๋ค์ ์ด๋๊ฐ์ ์กด์ฌํ๋ Object์ ๋ค์ด์๋ ๊ฐ์ ๋ชจ๋ ๊ฐ๋ค ์ธ ์ ์๋ค.