728x90
반응형
데코레이터는 클래스의 기능을 증강하는 함수로 여러 함수에서 공통으로 수행되는 부분을 데코레이터 만들어두면 좋습니다.
Decorators on a property, method, accessor
1. First argument is the prototype of the object
2. Second argument is the key of the property/method/accessor on the object
3. Third argument is the property descriptor
4. Decorators are applied when the code for this is ran(not when an instance is created)
Property Descriptor for Methods
1.writable: Whether or not this property can bo changed
2.enumerable: Whether or not this property get looped over by a 'for...in'
3.value: Current value
4.configurable: Property definition can be changed and property can be deleted
class Boat {
@testDecorator
color: string = 'red';
get formattedColor(): string {
return `This boats color is ${this.color}`;
}
@logError('Something bad!')
pilot(): void {
console.log('swish');
}
}
function testDecorator(target: any, key: string) {
console.log(target.color);
}
function logError(errorMessage: string) {//데코레이터 팩토리
return function (target: any, key: string, desc: PropertyDescriptor): void {
const method = desc.value;
desc.value = function () {
try {
method();
} catch (e) {
console.log(errorMessage);
}
};
};
}
728x90
반응형
'개발공부 일지 > 자바스크립트' 카테고리의 다른 글
타입스크립트)제너릭(Generics) (0) | 2023.10.09 |
---|---|
자바스크립트)Symbol (0) | 2023.10.08 |
자바스크립트) 객체의 참조 VS 얕은 복사 VS 깊은 복사 (0) | 2023.10.02 |
자바스크립트) map과 filter차이 (0) | 2023.06.22 |
자바스크립트) 자바스크립트 데이터 전송 및 post 데이터 받기 (0) | 2021.12.29 |
댓글