Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save szaranger/86bfdcfff495fa4d5cdd8403d3da8d54 to your computer and use it in GitHub Desktop.
Save szaranger/86bfdcfff495fa4d5cdd8403d3da8d54 to your computer and use it in GitHub Desktop.
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