class Person {
public readonly name: string = 'Malza';
private readonly country: string = 'Korea'; // 바로 초기화
public constructor(private _name: string, private age: number) {}
hello() {
this.country = 'America'; // readonly 이기 때문에 에러가 난다.
}
}
const p2: Person = new Person('malza', 29);
p2.name = 'yoon'; // readonly 이기 때문에 에러가 난다.
// 위의 예제 처럼과 constructor 안에서만 초기화가 가능한 것을 볼 수 있다.
class Person {
public readonly name: string = 'Malza';
private readonly country: string;
public constructor(private _name: string, private age: number) {
this.country = 'America';
}
}
const p2: Person = new Person('malza', 29);