클래스(Class)와 접근 제어자
자바스크립트의 클래스에 데이터 은닉(캡슐화)이라는 객체지향의 꽃을 피우게 해주는 접근 제어자(Modifiers)입니다.
class Employee {
public name: string; // 어디서든 접근 가능 (기본값)
private salary: number; // 이 클래스 { } 안에서만 몰래 접근 가능 (외부 노출 금지)
protected role: string; // 나랑 나를 상속받은 자식 클래스까지만 접근 가능
constructor(name: string, salary: number) {
this.name = name;
this.salary = salary;
this.role = "직원";
}
}
const e1 = new Employee("민수", 5000);
console.log(e1.name); // 통과
// console.log(e1.salary); // ❌ 에러! private 속성이라 밖에서 볼 수 없음!