readonly 속성: 읽기만 가능한 속성

초기값을 정하는 구간에서만 수정 가능하다.

 

예제

TS 파일

class Person55 {

  public readonly name: string = "Mark";
  private readonly country: string = "Korea";

  public constructor(private age: number, public _name: string){
    // 에러가 발생하지 않는다.
    this.country = "china";
  }

  // 에러 발생
  // hello(){
  //   this.country = "China";
  // }
}
const p12: Person55 = new Person55(22, 'aaa');

console.log(p12);
// readonly 이기 때문에 에러 발생
// p12.name = "수정";

 

+ Recent posts