๐ฉโ๐ ๊ฐ๋จํ ๊ฒ์ ๊ธฐ๋ฅ์ ๊ฐ์ง ์ค๋ธ์ ํธ๋ฅผ ๋ฝ๋ Class๋ฅผ ๋ง๋ ๋ค๋ ๊ฐ์
๐ ํ๋ ์ด ์กฐ๊ฑด
(1) ๋ชจ๋ Unit์ ์ธ์คํด์ค๋ ๊ณต๊ฒฉ๋ ฅ, ์ฒด๋ ฅ ์์ฑ์ด ์์ผ๋ฉฐ ๊ธฐ๋ณธ ๊ณต๊ฒฉ๋ ฅ์ 5, ๊ธฐ๋ณธ ์ฒด๋ ฅ์ 100์ผ๋ก ์ค์
(2) ๋ชจ๋ Unit์ ์ธ์คํด์ค๋ ์ ํฌ๋ ฅ์ ์ธก์ ํด์ฃผ๋ battlePoint๋ผ๋ getter๊ฐ ์๋ค.
battlePoint๋ฅผ ์คํํ๋ฉด ํ์ฌ ๊ณต๊ฒฉ๋ ฅ๊ณผ ์ฒด๋ ฅ์ ๋ํ ๊ฐ์ ์ฝ์์ฐฝ์ ์ถ๋ ฅํด์ฃผ์ด์ผ ํ๋ค.
(3) ๋ชจ๋ Unit์ ์ธ์คํด์ค๋ heal์ด๋ผ๋ setter๊ฐ ์์ต๋๋ค.
์ธ์คํด์ค.heal = 50 ์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ์ฒด๋ ฅ ์์ฑ์ด 50 ์ฆ๊ฐํด์ผํฉ๋๋ค.
๐ฉโ๐ ์ธ์คํด์ค๋ class๋ก๋ถํฐ ์๋ก์์ฑ๋๋ ์ค๋ธ์ ํธ๋ฅผ ๋ปํจ.
๐ ์์
class Unit {
constructor(){
this.์ฒด๋ ฅ = 100;
this.๊ณต๊ฒฉ๋ ฅ = 5;
}
get battlePoint(){ //getter์ ๊ทธ์ ์ถ๋ ฅ์ฉ return์ด ํ์
return this.์ฒด๋ ฅ + this.๊ณต๊ฒฉ๋ ฅ;
}
set heal(a){ //์ค๋ธ์ ํธ ๋ด์ฉ ์์ ์ฉ
this.์ฒด๋ ฅ = this.์ฒด๋ ฅ + a;
}
};
var ๋ด์บ๋ฆญํฐ = new Unit();
๋ด์บ๋ฆญํฐ.heal = 50;
console.log(๋ด์บ๋ฆญํฐ.battlePoint);