Last active
December 12, 2020 15:45
-
-
Save h4ck4life/80cc21524e180f1021bd53235eb1f32d to your computer and use it in GitHub Desktop.
A simple example of method decorator in Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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