Class Getter, Setter ํ๊ธฐ
class TestClass {
constructor(type, name, sound) { //๋ฐ์ดํฐ ์์ ํ ๋ ค๋ ์์๋ค๋ง ์ ๊ฒฝ์ด๋ค.
this.type = type;
this.name = name;
this.sound = sound;
};
get tion() {
let fruits = [this.type, this.name, this.sound];
return fruits.map((name, index) => { //json ํํ๋ก ๋ง๋ค์ด์ ๋ฐฐ์ด๋ก ๋ฐํํ๋ ํจ์
return {
id: index + 1,
name,
done: false
}
});
};
set tion(value) { // ํน์ ์์๋ฅผ ๋ณ๊ฒฝํ๊ณ ์ ํ๋ ํจ์!
this.name = value;
};
}
const purpleCuteSlime = new TestClass('๊ฐ๋๋ฆฌ','๋ฉ๋ฉ์ด', '๋ฉ๋ฉ');
purpleCuteSlime.tion = '์คํฐ๋ธ ๋ก์ ์ค'
console.log(purpleCuteSlime.tion) //์์๋ค ๋ชจ๋ ์กฐํ ํ๊ธฐ
console.log(purpleCuteSlime.tion[1].name) //์คํฐ๋ธ ๋ก์ ์ค ์กฐํํ๊ธฐ
// ๊ฒฐ๊ณผ)
[
{ id: 1, name: '๊ฐ๋๋ฆฌ', done: false },
{ id: 2, name: '์คํฐ๋ธ ๋ก์ ์ค', done: false },
{ id: 3, name: '๋ฉ๋ฉ', done: false }
]
์คํฐ๋ธ ๋ก์ ์ค