Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In Angular, decorators are functions that allow a service, directive or filter to be modified prior to its usage.
Create decorator
function log(target,name,descriptor) {
const original=descriptor.value;
descriptor.value=function(...args) {
console.log('this function is hacked')
const result=original.apply(this,args)
console.log("the result of the function is ", result);
return result;
}
original();
return descriptor;
}
Usage of decorator
@log
sum(a,b) {
return a+b;
}
//function overridden by decorator called
sum(2,3)
//output will be
this function is hacked
the result of the function is 5