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

 

1. ํ•จ์ˆ˜ ์„ ์–ธ

• ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฆฌํ„ด๊ฐ’์˜ ํƒ€์ž…์„ ์„ ์–ธ ํ›„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

function func1(name: string): string {
	return 'hello ${name}';
}

const func2 = function (name: string): string {
	return 'hello ${name}';
}

const func3 = (name: string): string => {
	return 'hello ${name}';
}

const world4 = (name: string): string => 'hello ${name}';

 

2. ๋งค๊ฐœ๋ณ€์ˆ˜ ์ˆ˜ ์ผ์น˜

 

• ํ•จ์ˆ˜์— ์ฃผ์–ด์ง„ ์ธ์ž์˜ ์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๊ธฐ๋Œ€ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜์™€ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.

function buildName(firstName: string, lastName: string) {
	return firstName + " " + lastName;
}

const result1 = buildName("Bob"); // Error: Expected 2 arguments, but got 1
const result2 = buildName("Bob", "Adams", "Sr."); // Error: Expected 2 arguments, but got 3
const result3 = buildName("Bob", "Adams")

 

3. Optional Parameter

• ?๋ฅผ ๋ถ™์—ฌ ์„ ํƒ์  ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งŒ๋“ค์ˆ˜๋„ ์žˆ๋‹ค.

• ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์€ ํ•„์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋’ค์— ์œ„์น˜ํ•ด์•ผํ•œ๋‹ค.

function buildName(firstName: string, lastName?: string) {
	if (lastName) return firstName + " " + lastName;
	else return firstName;	
}

let result1 = buildName("Bob");
let result2 = buildName("Bob", "Adams");
let result3 = buildName("Bob", "Adams", "Sr."); // Error: Expected 2 arguments, but got 3

 

4. Rest Parameter

• ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ƒ๋žต ๋ถ€ํ˜ธ(...) ๋’ค์˜ ์ธ์ž ๋ฐฐ์—ด์„ ๋นŒ๋“œํ•ด ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

• ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜๋ฅผ ๋ฌดํ•œ์œผ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.

• ์•„๋ฌด๊ฒƒ๋„ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.

function buildName1(firstName: string, ...restOfName: string[]) {
	// restOfName = [ 'Samuel', 'Lucas', 'MacKinzie' ]
	return firstName + " " + restOfName.join(" ");
}

let employeeName = buildName1("Joseph", "Samuel", "Lucas","MacKinzie");
// "Joseph Samuel Lucas MacKinzie"

 

5. Function Overloading

 ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํ˜•ํƒœ๊ฐ€ ์—ฌ๋Ÿฌ ์ผ€์ด์Šค์— ๋Œ€์‘ํ•˜๋Š” ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 ํ•จ์ˆ˜์˜ ๋‹คํ˜•์„ฑ(๋‹ค์–‘ํ•œ ํ˜•ํƒœ)์„ ์ง€์›ํ•œ๋‹ค.

 

ํ•จ์ˆ˜ ์˜ค๋ฒ„๋กœ๋”ฉ์„ ์œ„ํ•ด ํ•ด์•ผํ•  ๊ฒƒ 2๊ฐ€์ง€

1. ์„ ์–ธ : ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ค ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…๋“ค์„ ๋‹ค๋ฃฐ ๊ฒƒ์ธ์ง€ ์„ ์–ธ

2. ๊ตฌํ˜„ : ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์— ๋Œ€์‘ํ•˜๋Š” ๊ตฌ์ฒด์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ

 

1. ํ•จ์ˆ˜ ์˜ค๋ฒ„๋กœ๋”ฉ ์„ ์–ธ

  • ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๋Š” ์„œ๋กœ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
  • ๋ฐ˜ํ™˜ ํƒ€์ž…์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
class User {  // โœ…
  constructor(private id: string) {}
  setId(id: string): string;
  setId(id: number): string;
}

class User { // โŒ
  constructor(private id: string) {}
  // ์„ ์–ธ ์‹œ์— ์—๋Ÿฌ๋Š” ๋‚˜์ง€ ์•Š์ง€๋งŒ ์˜ค๋ฒ„๋กœ๋”ฉ ํ•จ์ˆ˜ ์ •์˜ ์‹œ์— ์—๋Ÿฌ;
  setId(id: string): void;
  setId(id: string): number; // ๋ฐ˜ํ™˜ ํƒ€์ž… ๋‹ค๋ฆ„;
  setId(radix: number, id: number): void; // ์ธ์ˆ˜ ์ˆœ์„œ ๋‹ค๋ฆ„
}

2. ํ•จ์ˆ˜ ์˜ค๋ฒ„๋กœ๋”ฉ ๊ตฌํ˜„

1) ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์„ ๋•Œ

class User {
  constructor(private id: string) {}
  setId(id: string): void;
  setId(id: number): void;
  setId(id: string | number): void {
    this.id = typeof id === 'number' ? id.toString() : id;
  }
}

2) ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅผ ๋•Œ

class User {
  constructor(private id: string) {}
  setId(id: string): void;
  setId(id: number, radix: number): void;
  setId(id: string | number, radix?: number): void {
    this.id = typeof id === 'number' ? id.toString(radix) : id;
  }
}

 

3. ํ•จ์ˆ˜ ์˜ค๋ฒ„๋กœ๋”ฉ์œผ๋กœ ๋ฐ”๊พผ ์˜ˆ์‹œ

Before

- ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž…๋งŒ ๋‹ค๋ฅด๊ณ  ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ์—์„œ ํ•จ์ˆ˜ ์˜ค๋ฒ„๋กœ๋”ฉ์„ ์ ์šฉํ•ด๋ณด์ž

const addZero = (num: string) => (num > 9 ? '' : '0') + num;

function formatDate(date: Date, format = 'yyyyMMdd'): string {
  const yyyy = date.getFullYear().toString();
  const MM = addZero(date.getMonth() + 1);
  const dd = addZero(date.getDate());
  return format.replace('yyyy', yyyy).replace('MM', MM).replace('dd', dd);
}

function formatDateString(dateStr: string, format = 'yyyyMMdd'): string {
  const date = new Date(dateStr);
  const yyyy = date.getFullYear().toString();
  const MM = addZero(date.getMonth() + 1);
  const dd = addZero(date.getDate());
  return format.replace('yyyy', yyyy).replace('MM', MM).replace('dd', dd);
}

function formatDateTime(datetime: string, format = 'yyyyMMdd'): string {
  const date = new Date(datetime);
  const yyyy = date.getFullYear().toString();
  const MM = addZero(date.getMonth() + 1);
  const dd = addZero(date.getDate());
  return format.replace('yyyy', yyyy).replace('MM', MM).replace('dd', dd);
}

After

const addZero = (num: string) => (num > 9 ? '' : '0') + num;

function formatDate(date: Date, format = 'yyyyMMdd'): string;
function formatDate(date: number, format = 'yyyyMMdd'): string;
function formatDate(date: string, format = 'yyyyMMdd'): string;

function formatDate(date: string | Date | number, format = 'yyyyMMdd'): string {
  const dateToFormat = new Date(date);
  // … dateToFormat validation …
  const yyyy = dateToFormat.getFullYear().toString();
  const MM = addZero(dateToFormat.getMonth() + 1);
  const dd = addZero(dateToFormat.getDate());
  return format.replace('yyyy', yyyy).replace('MM', MM).replace('dd', dd);
}

 

4. ์ œ๋„ค๋ฆญ๊ณผ ์ฐจ์ด์ 

  ํƒ€์ž… ์ถ”๋ก  ์‹œ์  ์šฉ๋„์˜ ๋ฒ”์œ„
์ œ๋„ค๋ฆญ ํƒ€์ž…์ด ์‚ฌ์šฉ๋˜๋Š” ์‹œ์  ์ œ๋„ค๋ฆญ ํƒ€์ž…
์ธํ„ฐํŽ˜์ด์Šค
ํด๋ž˜์Šค
ํ•จ์ˆ˜ ๋“ฑ
ํ•จ์ˆ˜ ์˜ค๋ฒ„๋กœ๋”ฉ ํƒ€์ž…์„ ์„ ์–ธํ•˜๋Š” ์‹œ์  ํ•จ์ˆ˜
๋ฉ”์„œ๋“œ
๋ฐ˜์‘ํ˜•