[JavaScript] ํด๋ž˜์Šค ES6

[์›๋ณธ ๋งํฌ]

ES6๋ถ€ํ„ฐ ํด๋ž˜์Šค ๋ฌธ๋ฒ•์ด ์ƒ๊ฒผ๋‹ค.

๊ทผ๋ฐ ๋ญ ์‚ฌ์‹ค ๋Œ€๋‹จํ•œ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ๊ฑด ์•„๋‹ˆ๊ณ , ๋‹จ์ˆœํžˆ ํŽธ์˜์„ฑ๊ณผ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์ถ”๊ฐ€๋œ๊ฑฐ๋ผ๊ณ  ํ•œ๋‹ค.

๋Œ€์ถฉ ์ด๋ ‡๊ฒŒ ์ƒ๊ฒผ๋‹ค.

class Person {
    //์ƒ์„ฑ์ž๋‹ค.
    constructor(name, age){
        this._name = name;
        this._age = age
    }

    //์ฝ๊ธฐ์ „์šฉ ํ”„๋กœํผํ‹ฐ๋‹ค.
    get Name() {
        return this._name;
    }
    //์“ฐ๊ธฐ์ „์šฉ ํ”„๋กœํผํ‹ฐ๋‹ค.
    set Name(name) {
        this._name = name;
    }

    get Age(){  
        return this._age; 
    }

    //๊ทธ๋ƒฅ ๋ฉ”์„œ๋“œ๋‹ค.
    AgeInc(){
        this._age++;
    }
}

var john = new Person('john', '3000');
john.Name='tom'; 
john.AgeInc()
console.log(john.Name, john.Age);

์ƒ์†๋„ ์ข€๋” ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ œ๊ณต๋œ๋‹ค.
์ ‘๊ทผ์ œ์–ด๋Š” ์•ˆ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค๋งŒ..

//Person์„ ์ƒ์†๋ฐ›๋Š” Student ํด๋ž˜์Šค
class Student extends Person
{
    constructor(name, age, school){
        super(name, age); //์ƒ์œ„ ํด๋ž˜์Šค ์ดˆ๊ธฐํ™”
        this._school = school;
    }

    get School() {
        return this._school; 
    }
}

var john = new Student('john', '3000', 'ํ˜ธ๊ทธ์™€ํŠธ');
console.log(john.Name, john.Age, john.School);