- 아래와 같이 매개변수에 초기화를 진행하여 코드의 수를 줄일 수 있다.
Before
class Department {
private id: string; ✅
public name: string; ✅
private employees: string[] = [];
constructor(id: string, n: string) {
this.id = id; ✅
this.name = n; ✅
}
describe(this: Department) {
console.log('Department' + this.name);
}
}
const accounting = new Department('Accounting');
After
class Department {
private employees: string[] = [];
constructor(private id: string, public name: string) {
}
describe(this: Department) {
console.log(`Department (${this.id}): ${this.name}`);
}
}
const accounting = new Department('d1', 'Accounting');