2024. 7. 1. 22:21ใTIL
๐ก ๋ฐ์ฝ๋ ์ดํฐ ( feat. ํ์ ์คํฌ๋ฆฝํธ )

- ๋ฐ์ฝ๋ ์ดํฐ๋ ํด๋์ค์ ๊ธฐ๋ฅ์ ์ฆ๊ฐํ๋ ํจ์๋ก ์ฌ๋ฌ ํจ์์์ ๊ณตํต์ผ๋ก ์ํ๋๋ ๋ถ๋ถ์ ๋ฐ์ฝ๋ ์ดํฐ๋ก ๋ง๋ค์ด ์ฌ์ฉํ๋ค.
- ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ์ฃผ์ ํ๊ฑฐ๋, ๊ธฐ์กด ๊ธฐ๋ฅ์ ์์ ํ๊ฑฐ๋, ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, ๋ฐ์ฝ๋ ์ดํฐ๋ ์ฃผ๋ก ์ฝ๋์ ๊ฐ๋ ์ฑ์ ๋์ด๊ณ , ๋ฐ๋ณต์ ์ธ ์์ ์ ์ค์ฌ์ฃผ๋ฉฐ, ์ฝ๋์ ์ ์ง๋ณด์์ฑ์ ํฅ์์ํค๋ ๋ฐ ์ ์ฉํ๋ค
1. ๋ฐ์ฝ๋ ์ดํฐ ๊ธฐ๋ณธ ๊ตฌ์กฐ
function ๋ฐ์ฝ๋ ์ดํฐ๋ช
(target: any, propertyKey?: string | symbol, descriptor?: PropertyDescriptor) { }
- target ์ ๋ฐ์ฝ๋ ์ดํฐ๊ฐ ์ ์ฉ๋ ์์๋ฅผ ๋ํ๋ธ๋ค. ex) ํด๋์ค, ๋ฉ์๋, ํ๋กํผํฐ ๋ฑ
- propertyKey๋ ๋ฐ์ฝ๋ ํฐ์ด๊ฐ ์ ์ฉ๋ ํ๋กํผํฐ๋ ๋ฉ์๋์ ์ด๋ฆ์ ๋ปํ๋ค.
- descriptor๋ ๋ฉ์๋๋ getter๋ setter์ ํ๋กํผํฐ ๋์คํฌ๋ฆฝํฐ๋ฅผ ๋ํ๋ธ๋ค.
2. ๋ฐ์ฝ๋ ์ดํฐ ์ ํ
1) ํด๋์ค ๋ฐ์ฝ๋ ์ดํฐ (Class Decorator)
- ํด๋์ค ๋ฐ์ฝ๋ ์ดํฐ๋ ํด๋์ค ์ ์ฒด์ ์ ์ฉ๋๋ฉฐ, ํด๋์ค๊ฐ ์์ฑ๋ ๋ ํน์ ์์ ์ ์ํํ๊ณ ์ถ์ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
function logClass(constructor: Function) {
console.log(`ํด๋์ค๊ฐ ์์ฑ๋์์ต๋๋ค: ${constructor.name}`);
}
@logClass
class Person {
constructor(public name: string) {}
}
- ๋ก๊ทธ ํด๋์ค ๋ฐ์ฝ๋ ์ดํฐ๊ฐ ์ฌ๋ person ํด๋์ค์ ์ ์ฉ๋์ด person ํด๋์ค๊ฐ ์์ฑ ๋ ๋๋ง๋ค ์ฝ์์ ๋ก๊ทธ๋ฅผ ์ถ๋ ฅ์ํจ๋ค.
2) ๋ฉ์๋ ๋ฐ์ฝ๋ ์ดํฐ (Method Decorator)
- ๋ฉ์๋ ๋ฐ์ฝ๋ ์ดํฐ๋ ํด๋์ค์ ํน์ ๋ฉ์๋์ ์ ์ฉ๋๋ฉฐ, ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋ ํน์ ์์ ์ ์ํํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ค.
function ์๊ฐ์ธก์ (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const ์๋ณธ๋ฉ์๋ = descriptor.value;
descriptor.value = function(...args: any[]) {
const ์์์๊ฐ = Date.now();
const ๊ฒฐ๊ณผ = ์๋ณธ๋ฉ์๋.apply(this, args);
const ์ข
๋ฃ์๊ฐ = Date.now();
console.log(`${propertyKey} ๋ฉ์๋ ์คํ ์๊ฐ: ${์ข
๋ฃ์๊ฐ - ์์์๊ฐ}ms`);
return ๊ฒฐ๊ณผ;
};
return descriptor;
}
class ๊ณ์ฐ๊ธฐ {
@์๊ฐ์ธก์
๋ํ๋ค(a: number, b: number) {
return a + b;
}
}
const ๋ด๊ณ์ฐ๊ธฐ = new ๊ณ์ฐ๊ธฐ();
๋ด๊ณ์ฐ๊ธฐ.๋ํ๋ค(5, 3);
// => ๋ํ๋ค ๋ฉ์๋ ์คํ ์๊ฐ: Xms
- ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋๋ง๋ค ๋ฐ์ฝ๋ ์ดํฐ๊ฐ ์๊ฐ์ ์ธก์ ํ๋ค.
3) ์ ๊ทผ์ ๋ฐ์ฝ๋ ์ดํฐ (Accessor Decorator)
- ์ ๊ทผ์ ๋ฐ์ฝ๋ ์ดํฐ๋ ํด๋์ค์ ํน์ ์ ๊ทผ์(ex. getter/setter )์ ์ ์ฉ๋๋ค. ์ ๊ทผ์๊ฐ ํธ์ถ๋ ๋ ํน์ ์์ ์ ์ํํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ค.
function ๋ก๊ทธ์ ๊ทผ์(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const ์๋ณธGetter = descriptor.get;
descriptor.get = function() {
console.log(`์ ๊ทผ์๊ฐ ํธ์ถ๋์์ต๋๋ค: ${propertyKey}`);
return ์๋ณธGetter?.apply(this);
};
return descriptor;
}
class person {
private _age: number;
constructor(age: number) {
this._age = age;
}
@๋ก๊ทธ์ ๊ทผ์
get age() {
return this._age;
}
}
const john = new person(30);
console.log(john.age);
4) ํ๋กํผํฐ ๋ฐ์ฝ๋ ์ดํฐ (Property Decorator)
- ํ๋กํผํฐ ๋ฐ์ฝ๋ ์ดํฐ๋ ํด๋์ค์ ํน์ ํ๋กํผํฐ์ ์ ์ฉ๋๋ฉฐ, ํ๋กํผํฐ๊ฐ ์ ์ธ๋ ๋ ํน์ ์์ ์ ์ํํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ค.
function logProperty(target: any, propertyKey: string) {
console.log(`ํ๋กํผํฐ๊ฐ ์ ์ธ๋์์ต๋๋ค: ${propertyKey}`);
}
class Book {
@logProperty
title: string;
constructor(title: string) {
this.title = title;
}
}
const myBook = new Book('ํ์
์คํฌ๋ฆฝํธ ์๋ฒฝ ๊ฐ์ด๋'); // =>ํ๋กํผํฐ๊ฐ ์ ์ธ๋์์ต๋๋ค: title
'TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TIL (DTO) (0) | 2024.07.03 |
---|---|
TIL( TypeScript - Utility type ) (0) | 2024.07.02 |
TIL ( prisma ์์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค(MySQL) ์ฐ๊ฒฐํ๊ธฐ ) (0) | 2024.06.28 |
TIL( TypeScriptํ์ต ) (0) | 2024.06.27 |
TIL ( TypeScript ํ์ต) (0) | 2024.06.26 |