Hi

es2015 클래스 본문

WEB(웹)/javascript

es2015 클래스

SharingWorld 2018. 10. 26. 09:18

클래스

정적 메서드(Static Method), 인스턴스 메서드(Instance Method), 생성자(Constructor)를 모두 잘 지원하고 있습니다.

class Person {
    constructor(name, tel, address) {
        this.name = name;
        this.tel = tel;
        this.address = address;
        if (Person.count) { Person.count++; } else { Person.count = 1; }    
    }
    static getPersonCount() { 
        return Person.count; 
    }
    toString() {
        return `name=${this.name}, tel=${this.tel}, address=${this.address}`;
    }
}

var p1 = new Person('이몽룡', '010-222-3332', '경기도');
var p2 = new Person('홍길동', '010-222-3333', '서울');
console.log(p1.toString());
console.log(Person.getPersonCount());

상속도 지원합니다.

class Employee extends Person {
    constructor(name, tel, address, empno, dept) {
        super(name,tel,address);
        this.empno = empno;
        this.dept = dept;
    }
    toString() {
        return super.toString() + `, empno=${this.empno}, dept=${this.dept}`;
    }
    getEmpInfo() {
        return `${this.empno} : ${this.name}${this.dept} 부서입니다.`;
    }
}

let e1 = new Employee("이몽룡", "010-222-2121", "서울시", "A12311", "회계팀");

console.log(e1.getEmpInfo());
console.log(e1.toString());
console.log(Person.getPersonCount());


'WEB(웹) > javascript' 카테고리의 다른 글

es2015 전개 연산자  (0) 2018.10.26
javascript 모듈 (import, export)  (0) 2018.10.26
javascript apply  (0) 2018.10.25
babel 하는 법  (0) 2018.08.10
slice() 메소드  (0) 2018.07.29