Last active
January 22, 2016 09:50
-
-
Save frontdevops/7677f3015bd0407ac8ef to your computer and use it in GitHub Desktop.
TypeScript static property decorator example
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
/** | |
* Декоратор для логирования статических свойств | |
* @param target | |
* @param key | |
*/ | |
function log_static_prop(target:any, key:string):any { | |
var val = target[key]; | |
return { | |
get: ():any => { | |
console.log(`Get: ${key} => ${val}`); | |
return val; | |
}, | |
set: (newVal):void => console.log(`Set: ${key} => ${newVal}`) | |
} | |
} | |
class Person { | |
public name:string; | |
public surname:string; | |
@log_static_prop public static vip:boolean = false; | |
constructor(name:string, surname:string) { | |
this.name = name; | |
this.surname = surname; | |
} | |
} | |
let me = new Person("Alex", "Mayorov"); | |
Person.vip; | |
Person.vip = true; | |
Person.vip; | |
/* Output: | |
Get: vip => false | |
Set: vip => true | |
Get: vip => false | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment