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. ์ ๋ค๋ฆญ๊ณผ ์ฐจ์ด์
ํ์ ์ถ๋ก ์์ | ์ฉ๋์ ๋ฒ์ | |
์ ๋ค๋ฆญ | ํ์ ์ด ์ฌ์ฉ๋๋ ์์ | ์ ๋ค๋ฆญ ํ์
์ธํฐํ์ด์ค ํด๋์ค ํจ์ ๋ฑ |
ํจ์ ์ค๋ฒ๋ก๋ฉ | ํ์ ์ ์ ์ธํ๋ ์์ | ํจ์ ๋ฉ์๋ |