Created
November 12, 2021 00:29
-
-
Save szaranger/86bfdcfff495fa4d5cdd8403d3da8d54 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export class Person { | |
names = []; | |
@Confirmable('Are you sure?') | |
@Confirmable('Are you super, super sure? There is no going back!') | |
addName(name) { | |
this.names.push(name); | |
} | |
} | |
// Method Decorator | |
function Confirmable(message: string) { | |
return function (target: Object, key: string | symbol, descriptor: PropertyDescriptor) { | |
const original = descriptor.value; | |
descriptor.value = function( ... args: any[]) { | |
const allow = confirm(message); | |
if (allow) { | |
const result = original.apply(this, args); | |
return result; | |
} else { | |
return null; | |
} | |
}; | |
return descriptor; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment