๐ย Reference
๐ย Chapter
Class
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์ ๋ค์ด์๋ ๊ฐ์ ๋ชจ๋ ๊ฐ๋ค ์ธ ์ ์๋ค.
- ์ฆ, eyes์ nose๋ฅผ ์ด๋๊ฐ์ ์๋ ๋น ๊ณต๊ฐ์ ๋ฃ์ด๋๊ณ kim๊ณผ park์ด ๊ณต์ ํด์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
Prototype Object