๋ฐ์ํ
prototype ์ ์์ฑ๋ ๊ฐ์ฒด๋ค์ด ์ฌ๋ฌ๊ฐ ์์ด๋ ๋์ผํ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์์ ํจ์จ์ ์ผ๋ก ์ฌ์ฌ์ฉ๋จ
๊ฐ์ฒด๋ค์ด ๊ฐ์ ๊ณต๊ฐ์ shareํ๋ ๊ฒ prototype
โ this ๋ก function ์ ์ ํ์ ๊ฒฝ์ฐ
const Health = function(name, healthTime) {
this.name = name;
this.healthTime = healthTime;
this.showHealth = function() {
return this.name + this.lastTime + "์ด๋์๋ฃ";
}
}
const h1 = new Health("tami", "12:12");
const h2 = new Health("recoder", "02:12");
h1.showHealth === h2.showHealth // false
โ Prototype์ผ๋ก ์ ์ํ ๊ฒฝ์ฐ
const Health = function(name, healthTime) {
this.name = name;
this.healthTime = healthTime;
}
Health.prototype.showHealth = function() {
console.log(this.name + this.healthTime + "์ด๋์๋ฃ";
}
const h1 = new Health("tami", "12:12");
const h2 = new Health("recoder", "02:12");
h1.showHealth === h2.showHealth // true
h1 ๊ณผ h2 ๋ ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋๋ฌ์๋ showHealth ๋ฅผ ๊ณต์ ํ๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ ํจ์จ ์์น
๋ฉ์๋ == prototype์ ๋ง์ด ๋ณด๊ดํ๋ ํธ
'๐ JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Javascript JSON.parse(), JSON.stringify() ์ฌ์ฉํ๋๋ฒ (0) | 2021.02.23 |
---|---|
Node.JS express๋ก ์๋ฒ ๊ตฌ์ฑํ๊ธฐ (5) | 2021.02.23 |
์ด๋ฒคํธ ๋ฒ๋ธ๋ง, ์ด๋ฒคํธ ์บก์ณ๋ง - DOM์ ์ด๋ฒคํธ ํธ๋ค๋ง (0) | 2021.02.22 |
[JavaScript ] ์ด๋ฒคํธํธ๋ค๋ง (0) | 2021.02.16 |
[JavaScript] DOM์ด๋ ๋ฌด์์ธ๊ฐ? (0) | 2021.02.16 |
๋๊ธ