2020. 10. 1. 23:28, Developer
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;
'Developer' 카테고리의 다른 글
[typescript]Generic class (0) | 2020.10.02 |
---|---|
[typescript]Generic (0) | 2020.10.02 |
[typescript]class - getter와 setter (0) | 2020.10.01 |
[typescript]class - 자식 생성자는 super를 꼭 가져야함 (0) | 2020.10.01 |
[typescript]class - class안에 생성자의 private 변수 (0) | 2020.10.01 |
Comments, Trackbacks