private : ์์ฑ๋ ๊ฐ์ฒด ๋ด๋ถ์์๋ง ์ ๊ทผ ๊ฐ๋ฅํ ์์ฑ
protected : private๊ณผ ๋น์ทํ์ง๋ง private๊ณผ ๋ค๋ฅธ ์ ์ ์ด ํด๋์ค์์๋ฟ ์๋๋ผ ์ด ํด๋์ค๋ฅผ ํ์ฅํ๋ ๋ชจ๋ ํด๋์ค์์๋ ์ฌ์ฉ ๊ฐ๋ฅ ํจ
readonly ํค์๋
- ์ด๊ธฐํ ์ค์ ํ ๋ฒ๋ง ์ฌ์ฉ ๊ฐ๋ฅ
- ํน์ ์์ฑ์ด ์ด๊ธฐํ ๋๊ณ ๋๋ฉด ์ดํ์๋ ๋ณ๊ฒฝ๋์ด์๋ ์๋จ
- ์ถ๊ฐ์ ์ธ ํ์ ์์ ์ฑ์ ๋ํด์ฃผ๊ณ ์๋๋ฅผ ๋ช ํํ๊ฒ ํด์ค
- ์ฝ์ ์ด๊ธฐํ
// ์ด๊ธฐํ ์ฝ์ ์
class Department {
private id: string;
private name: string;
private employees: string[] = [];
constructor(id: string, name: string) {
this.id = n;
this.name = n;
}
}
// ์ด๊ธฐํ ์ฝ์ ํ
class Department {
private employees: string[] = [];
constructor(private readonly id: string, public name: string) {
}
}
// ์ด๊ธฐํ ์ฝ์ ์
class Product {
title: string;
price: number;
private isListed: boolean;
constructor(name: string, pr: number) {
this.title = name;
this.price = pr;
this.isListed = true;
}
}
// ์ด๊ธฐํ ์ฝ์ ํ
class Product {
private isListed: boolean;
constructor(public title: string, public price: number) {
this.isListed = true;
}
}
this ํค์๋
- this ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌด์ธ๊ฐ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ super๋ฅผ ๋จผ์ ํธ์ถํด์ผ ํจ
getter / setter
- ๋ก์ง์ ์บก์ํํ๊ณ ์์ฑ์ ์ฝ๊ฑฐ๋ ์ค์ ํ๋ ค ํ ๋
- ์คํ๋์ด์ผ ํ๋ ์ถ๊ฐ์ ์ธ ๋ก์ง์ ์ถ๊ฐํ๋๋ฐ ์ ์ฉํจ
์ ์ ๋ฉ์๋ & ์์ฑ
- ์ ์ ๋ฉ์๋: ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋ ๊ฐ์ฒด๊ฐ ์๋๋ผ ํด๋์ค์์ ์ง์ ํธ์ถํ๋ ๋ฉ์๋
- ์ ์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํด๋์ค์ ์ธ์คํด์ค์์ ์ ๊ทผํ ์ ์๋
- ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ํด๋์ค์ ์ถ๊ฐํ ์ ์์
- ํด๋์ค ์ด๋ฆ์ ๋จผ์ ํธ์ถํ์ง ์๊ณ ํด๋์ค์ ์ง์ ์ ๊ทผ
- ์ฃผ๋ก ๋ ผ๋ฆฌ์ ์ผ๋ก ๊ทธ๋ฃนํํ๊ฑฐ๋ ํด๋์ค์ ๋งคํํ๋ ค๋ ์ ํธ๋ฆฌํฐ ํจ์๋
- ํด๋์ค์ ์ ์ฅํ๊ณ ์ ํ๋ ์ ์ญ ์์์ ์ฌ์ฉ ๋จ
- ๋ฉ์๋ ์์ static ํค์๋ค๋ฅผ ์ถ๊ฐํด ๋ค์ ๊ฐ์ฒด๋ฅผ ๋ฐํ
static createEmployee(name: string) {
return { name: name };
}
const employee1 = Department.createEmployee("Yumi");
console.log(employee1);
- ์ ์ ์์ฑ์ ์ธ์คํด์ค์์ ์ ํจํ์ง ์์
- ์ ์ ์์ฑ๊ณผ ์ ์ ๋ฉ์๋์ ์ ์ฒด์ ์ธ ๊ฐ๋ ์ ์ธ์คํด์ค์ ๋ถ๋ฆฌ๋์ด ์๊ธฐ ๋๋ฌธ
- ๊ทธ๋์ this ํค์๋๋ฅผ ์ฌ์ฉํด ์ ๊ทผํ๋ ๊ฑด ๋ถ๊ฐ๋ฅ
์ถ์ ํด๋์ค
- ์ถ์ ํด๋์ค : ์ธ์คํด์คํ๋ ์ ์๊ณ ํ์ฅ๋์ด์ผ ํ๋ ํด๋์ค
- ๋ฉ์๋ ์์ abstract๊ฐ ์๋ ๋ฉ์๋๊ฐ ํ๋ ์ด์์ด๋ผ๋ฉด
- ํด๋น ํด๋์ค ์์๋ abstract๋ฅผ ์ถ๊ฐ ํด์ผ ํจ
abstract class Department {
static fiscalYear = 2022;
// private id: string;
// private name: string;
protected employees: string[] = [];
constructor(protected readonly id: string, public name: string) {
// this.id = n;
// this.name = n;
}
abstract describe(this: Department): void;
}
- ์ถ์ ํด๋์ค๋ ์ผ๋ถ ์์ ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ ๋ชจ๋ ํด๋์ค๊ฐ
- ์ผ๋ถ ๊ณตํต ๋ฉ์๋ ๋๋ ์์ฑ์ ๊ณต์ ํ๋ ๊ฒฝ์ฐ ์ฌ์ฉ
- abstract ํค์๋๋ก ํ๊ธฐ๋ ํด๋์ค๋ค์ ์์ฒด์ ์ผ๋ก ์ธ์คํด์คํํ ์ ์์
์ฑ๊ธํค & ๊ฐ์ธ ์์ฑ์
static getInstance() {
// ์ด๋ฏธ ์ธ์คํด์ค๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ฉด ์ด๋ฅผ ๋ฐํ
if (AccountingDepartment.instance){
return this.instance
} // ๊ทธ๋ ์ง ์์ผ๋ฉด ์ ์ธ์คํด์ค๋ฅผ ์์ฑ
this.instance = new AccountingDepartment('d2',[])
return this.instance
}
// AccountingDepartment์ ์ ์ธ์คํด์ค๊ฐ ๋ฐํ
const accounting = AccountingDepartment.getInstance();
const accounting2 = AccountingDepartment.getInstance();
- ์ฑ๊ธํค : “new”๋ก ์์ฑํ์ง ์๊ณ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๊ตฌ์ฑํ๋ฏ๋ก ํน์ ์์ ์ ๋ฐ๋์
- ๋จ ํ๋์ ํด๋์ค ์ธ์คํด์ค๊ฐ ์กด์ฌ
์ธํฐํ์ด์ค
- ๊ฐ์ฒด์ ๊ตฌ์กฐ๋ฅผ ์ค๋ช ํจ
- ๊ตฌ์ฒด์ ์ธ ๊ฐ์ด ์๋ ๊ตฌ์กฐ๋ง ์์
interface Person {
name: string;
age: number;
greet(phrase: string): void;
}
- ์ธ์์ ์ด๋ฆ๊ณผ ํ์ , ๋ฉ์๋์ ๋ฐํ ํ์ ์์
interface Person {
name: string;
age: number;
greet(phrase: string): void;
}
let user1: Person;
user1 = {
name: "yum",
age: 20,
greet(phrase: string) {
console.log(phrase + "" + this.name);
},
};
user1.greet("Hi there - I am ");
- ์ธํฐํ์ด์ค ๋ด์ readonly ์ ์ด์๋ ์ถ๊ฐ ๊ฐ๋ฅ
- ํ์ง๋ง public, private ๋ฑ์ ์ง์ ๋ถ๊ฐ๋ฅ
- ํจ์์ ๊ตฌ์กฐ๋ฅผ ์ ์ํ๋๋ฐ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
์ ํ์ ๋งค๊ฐ๋ณ์ & ์์ฑ
interface Named {
readonly name?: string;
outputName?: string
}
ํด๋์ค, ์ธํฐํ์ด์ค ์ฐจ์ด์
- ์ธํฐํ์ด์ค๋ ์ธ์คํด์คํํ ์ ์์ผ๋ฉฐ ์ปดํ์ผ๋์ง ์๋ ๋ฐ๋ฉด,
- ํด๋์ค๋ ์ธ์คํด์คํํ ์ ์์ผ๋ฉฐ ์ปดํ์ผ ๋จ
LIST
'#1 Language ๐ > 1-3 Typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Typescript] ์ ๋ค๋ฆญ Generic <T> (1) | 2023.12.23 |
|---|---|
| [Typescript] ๊ณ ๊ธ ํ์ (1) | 2023.12.23 |
| [Typescript] ๊ตฌ์กฐ ๋ถํด (1) | 2023.12.23 |
| [Typescript] TypeScript ์ปดํ์ผ๋ฌ(๋ฐ ๊ตฌ์ฑ) (1) | 2023.12.23 |
| [Typescript] Typescript ๊ธฐ๋ณธ & ๊ธฐ๋ณธ ํ์ (1) | 2023.12.23 |