Class Getter, Setter

Class Getter, Setter ํ•˜๊ธฐ

์—ฌ๊ธฐ์„œ๋Š” ๋ฐ์ดํ„ฐ์›์†Œ, json ํ˜•ํƒœ, (setterํ•จ์ˆ˜ ,getterํ•จ์ˆ˜)๋ฅผ ์•Œ์•„๋‘๋Š”๊ฒƒ์ด ํฌ์ธํŠธ ์ž…๋‹ˆ๋‹ค.

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 }
]
์Šคํ‹ฐ๋ธ ๋กœ์ €์Šค

Last updated