TS Method Decorator

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2020 Nov 24 5:46
Editor
Edited
Edited
2025 Jun 4 9:46
Refs
Refs
function methodDecorator() { return function (target: any, property: any, descriptor: any) { let originMethod = descriptor.value descriptor.value = function (...args) { console.log('before') originMethod.apply(this, args) console.log('after') } } } @classDecorator("param1", "param2") class Test { property = "property"; hello: string; constructor(m: string) { this.hello = m; } @methodDecorator() test() { console.log("test") } }
메서드를 호출할 때 apply를 이용하여 this를 바인딩해줘야
이것을 이용하여 특정 메서드의 실행 전/후 로깅, 실행시간을 측정 가능
 
TS Method Decorator Target
TS Method Decorator Property
TS Method Decorator Descriptor
 
 

Recommendations