[์์ฑ์]
1. ๋ชจ๋ class๋ constructor๋ฅผ ๊ฐ์ง
2. constructor๋ class๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํธ์ถ๋๋ฉฐ ๊ฐ์ฒด์ ์ด๊ธฐํ๋ฅผ ๋ด๋น
ํด๋์ค์์ ์ปจ์คํธ๋ญํฐ๋ฅผ ๋ง๋ค๊ณ
์ค๋ธ์ ํธ๋ฅผ ์์ฑํ ๋๋ ํ๋ผ๋ฉํฐ๋ก ๋ฃ๊ธฐ๋ง ํ๋ฉด ๋๋ ์ฅ์ ์ด ์์ ์ด๊ฑฐ ์๋ฐ๋ ๋๊ฐ์๋ฐ
[access modifiers ์ ๊ทผ์ ํ์]
ํด๋ผ์ค ์ ๋ฉค๋ฒ ๋ณ์(ํ๋กํผํฐ)์
๋ฉ์๋์ ์ ์ฉ๋ ์ ์๋ ํค์๋
ํด๋ผ์ค ์ธ๋ถ๋ก๋ถํฐ์ ์ ๊ทผ์ ํต์
public: class์ ์ธํ ๋ ๋ํดํธ์
private: ํด๋ผ์ค๋ด์์๋ง ์ ๊ทผ ๊ฐ๋ฅ
protcted: ํด๋ผ์ค๋ด๋ถ, ์์๋ฐ์ ์์ ํด๋ผ์ค์์ ์ ๊ทผ ๊ฐ๋ฅ
ํด๋์ค๋ด ํ๋กํผํฐ์์๋ ์ ๊ทผ์ ํ์๋ฅผ ์ฌ์ฉํ ์ ์์
private fullname: string ํ๋ฉด
1) ์ธ๋ถํด๋์ค์์ ์ฝ๊ธฐ/์์ ๋ถ๊ฐ๋ฅ
2) ๊ทธ๋์ getter๋ฅผ ์ ์ธํด์ ๊ทธ ๋ณ์๋ฅผ ํ๋ฆฐํธํ๊ธฐ๋ก ์ฝ๊ธฐ ๊ฐ๋ฅ
3) ๊ทธ๋์ setter๋ฅผ ์ ์ธํด์ ๊ทธ ๋ณ์๋ฅผ ์์ ํ ์ ์์
private _fullname: string (๋น๊ณต๊ฐ๋ณ์ ์๋ฌต์ ์ปจ๋ฒค์
)
constructor์ ๋งค๊ฒ๋ณ์์ access modifiers์ง์ ์ ์ฉ
๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ์ปจ์คํธ๋ญํฐ์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ ๊ฐ์ ๊ฐ์ฒด์ ํ๋กํผํฐ ๊ฐ์ผ๋ก ์๋์ผ๋ก ๊ฐ์ด ์ด๊ธฐํ ๋๊ณ ํ ๋น
constructor is a method when making a class
ํด๋์ค๊ฐ ์์ํ ๋ ๋ง๋ค ํธ์ถ๋๋ค.
class Human {
public name: string;
public age: number;
public gender: string;
constructor(name:string, age:number, gender:string){
this.name = name;
this.age= age;
this.gender = gender;
}
}
const myLove = new Human("myLove", 26 , "Female")
'๐ Front > ๐ซ Typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React+Typescript] React.FC๋ฅผ ๊ตณ์ด ์ฌ์ฉํ์ง ์์๋ ๋๋ ์ด์ (0) | 2021.09.06 |
---|---|
[Typescript]: type, interface, generics ์ฌ์ฉํ๊ธฐ (0) | 2021.09.06 |
Typescript: ์ค๊ธ(Type Aliases/Type Guard) (0) | 2021.09.06 |
[Typescript] ์ถ๊ฐ ์ ๊ณต ํ์ (tuple, enum, any, void, never) (0) | 2021.09.06 |
[Typescript] ํ์ ์คํฌ๋ฆฝํธ ์ ์จ์? (0) | 2021.07.13 |