Skip to content

Instantly share code, notes, and snippets.

@umayr
Created August 31, 2016 03:03
Show Gist options
  • Save umayr/d03e040e511a70654a809a2f111da3f6 to your computer and use it in GitHub Desktop.
Save umayr/d03e040e511a70654a809a2f111da3f6 to your computer and use it in GitHub Desktop.
'use strict';
function classDecorator(target: any) {
console.log('class decorator');
}
function propertyDecorator(target: any, key: string | symbol) {
console.log('property decorator');
}
function staticMethodDecorator(target: any, key: string | symbol, descriptor: PropertyDescriptor) {
console.log('static method decorator');
}
function parameterDecorator(target: any, key: string | symbol, index: number) {
console.log('parameter decorator');
}
function methodDecorator(target: any, key: string, descriptor: PropertyDescriptor) {
let fn = descriptor.value;
descriptor.value = function(...args: any[]) {
console.log('pre method');
fn.apply(this, args);
console.log('post method');
}
console.log('method decorator');
}
@classDecorator
class Foo {
@propertyDecorator
foo: string;
@methodDecorator
fn(@parameterDecorator arg?: string) {
this.foo = arg;
console.log('method call');
}
@staticMethodDecorator
static fn () {
console.log('static method call');
}
}
let f = new Foo();
f.fn('bah');
Foo.fn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment