Life Developer
인생 개발자
[typescript]class - readonly

readonly를 사용하면 생성자로 값 세팅가능하고 그외방법으로 값을 넣지 못함.

그리고 public일때도 외부에서 변수 읽기만 가능.

 

 

class Person{

    private readonly _name:string =null;

    public readonly age:number=30;

 

    constructor(name:string){

        this._name=name;

    }

 

    public setName(name:string){

        //this._name=name;

    }

}

 

const p:person = new Person('kim');

console.log(p.age);

//p.age=36;

  Comments,     Trackbacks