FE/Typescipt

[Typescript] Class

따봉치치 2023. 9. 1. 18:37
접근 제어자(Acess Modifier)
  1. public : default, 어디서나 자유롭게 접근 가능
  2. protected : 나와 파생된 후손 클래스 내에서 접근 가능
  3. private : 클래스 내에서만 접근 가능
class userA {
	constructor(public first : string, protected last : string,private age : number) {
		this.first = first
		this.last = last
		this.age = age
	}
}

class UserB extends UserA() {
	getAge() {
		return `${this.first} ${this.last} is ${this.age}` //first, last 사용 가능 but age는 접근 불간ㅇ
	}
}

const neo = new UserA('Neo', 'Tom', 12)
neo.last //불가