Skip to content

Instantly share code, notes, and snippets.

@h4ck4life
Last active December 12, 2020 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h4ck4life/80cc21524e180f1021bd53235eb1f32d to your computer and use it in GitHub Desktop.
Save h4ck4life/80cc21524e180f1021bd53235eb1f32d to your computer and use it in GitHub Desktop.
A simple example of method decorator in Typescript
class Greeter {
@ChangeName('Ammara')
greet(greeting: string) {
return "Hello, " + greeting;
}
}
function ChangeName(value: string) {
return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
var originalMethod = descriptor.value;
descriptor.value = function(...args: any) {
return "Hello, " + value;
}
return descriptor;
};
}
const person = new Greeter();
console.log(person.greet('Alif'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment