
public 키워드
class User {
public name: string;
constructor(){
this.name = 'kim';
}
}
let 유저1 = new User();
유저1.name = 'park'; //가능
속성값을 어디서나 수정할 수 있음.
private 키워드
class User {
public name :string;
private familyName :string;
constructor(){
this.name = 'kim';
let hello = this.familyName + '안녕'; //가능
}
}
let 유저1 = new User();
유저1.name = 'park'; //가능
유저1.familyName = 456; //에러 발생
class { } 안에서만 속성값 수정 가능
=> 외부에서 속성이나 함수를 숨기고 싶을 때 사용.
(캡슐화)
private 수정방법
class User {
public name :string;
private familyName :string;
constructor(){
this.name = 'kim';
let hello = this.familyName + '안녕';
}
changeSecret(){
this.familyName = 'park';
}
}
let 유저1 = new User();
유저1.familyName = 'park'; //에러 발생
유저1.changeSecret() //가능
1. class안쪽에서 함수를 선언한 뒤,
2. 바깥쪽에서 인스턴스 변수를 통해 함수를 호출.
3. 간접적으로 private변수값 수정 가능.
자바스크립트에 없는 캡슐화를 타입스크립트에서는 사용하는 것을 보면 확실히 엄격한 놈이지만
내가 자바와 가깝게 지낸탓인지 매우 매우! 반가웠던 강의였다.
'TypeScript' 카테고리의 다른 글
import export, namespace 문법 (0) | 2023.07.04 |
---|---|
protected, static 키워드 (0) | 2023.07.03 |
Narrowing 추가적인 방법들 (0) | 2023.07.03 |
rest parameter , spread operator, destructuring 타입지정 (0) | 2023.07.03 |
interface vs type (0) | 2023.07.03 |

public 키워드
class User {
public name: string;
constructor(){
this.name = 'kim';
}
}
let 유저1 = new User();
유저1.name = 'park'; //가능
속성값을 어디서나 수정할 수 있음.
private 키워드
class User {
public name :string;
private familyName :string;
constructor(){
this.name = 'kim';
let hello = this.familyName + '안녕'; //가능
}
}
let 유저1 = new User();
유저1.name = 'park'; //가능
유저1.familyName = 456; //에러 발생
class { } 안에서만 속성값 수정 가능
=> 외부에서 속성이나 함수를 숨기고 싶을 때 사용.
(캡슐화)
private 수정방법
class User {
public name :string;
private familyName :string;
constructor(){
this.name = 'kim';
let hello = this.familyName + '안녕';
}
changeSecret(){
this.familyName = 'park';
}
}
let 유저1 = new User();
유저1.familyName = 'park'; //에러 발생
유저1.changeSecret() //가능
1. class안쪽에서 함수를 선언한 뒤,
2. 바깥쪽에서 인스턴스 변수를 통해 함수를 호출.
3. 간접적으로 private변수값 수정 가능.
자바스크립트에 없는 캡슐화를 타입스크립트에서는 사용하는 것을 보면 확실히 엄격한 놈이지만
내가 자바와 가깝게 지낸탓인지 매우 매우! 반가웠던 강의였다.
'TypeScript' 카테고리의 다른 글
import export, namespace 문법 (0) | 2023.07.04 |
---|---|
protected, static 키워드 (0) | 2023.07.03 |
Narrowing 추가적인 방법들 (0) | 2023.07.03 |
rest parameter , spread operator, destructuring 타입지정 (0) | 2023.07.03 |
interface vs type (0) | 2023.07.03 |