Created
August 31, 2016 03:03
-
-
Save umayr/d03e040e511a70654a809a2f111da3f6 to your computer and use it in GitHub Desktop.
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
'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