class Department {
name: string;
constructor(n: string){
this.name = n;
}
}
문제점
class Department{
private employees: string[] = [];
constructor(public name: string) {
}
}
해결방법
class Department{
name: string;
employees: string[] = [];
constructor(n: string) {
this.name = n;
}
describe (this: Department) {
console.log('Department: ' + this.name)
}
class Department{
name: string;
employees: string[] = [];
constructor(n: string) {
this.name = n;
}
describe (this: Department) {
console.log('Department: ' + this.name)
}
const accounting = new Department('Accounting');
문제점
accounting.employees[0] = 'Anna';
해결방법
private employees: string[] = [];
pulic은 default값으로 따로 설정해주지 않으면 자동으로 public이 프로퍼티가 된다.
pulic으로 설정된 프로퍼티는 직접 접근할 수 있다.
자바스크립트는 이해하지 못하는 키워드로 런타임 중에는 에러를 발생하지 않는다.
class Department{
private employees: string[] = [];
constructor(private readonly id: string, public name: string) {
}
describe (this: Department) {
console.log(`Department (${this.id}):`+ this.name)
}
addEmployee(employee: string){
this.employees.push(employee)
}
printEmployeeInformation() {
console.log(this.employees.length)
console.log(this.employees)
}
}