๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

#1 Language ๐Ÿ‘„/1-3 Typescript

[Typescript] ํด๋ž˜์Šค & ์ธํ„ฐํŽ˜์ด์Šค

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