Created
March 26, 2013 05:12
-
-
Save minodisk/5243300 to your computer and use it in GitHub Desktop.
onや_onから始まるとthisで束縛するクラス
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
| class Class { | |
| constructor() { | |
| var ctor:any = (<any>this).constructor; | |
| if (ctor.__on__ == null) { | |
| ctor.__on__ = {}; | |
| for (var m in this) { | |
| var fn:any = this[m]; | |
| if (typeof fn === 'function' && | |
| (m.indexOf('on') === 0 || m.indexOf('_on') === 0)) { | |
| ctor.__on__[m] = fn; | |
| } | |
| } | |
| } | |
| for (var m in ctor.__on__) { | |
| ((m:string, fn:Function)=> { | |
| this[m] = ()=> { | |
| return fn.apply(this, Array.prototype.slice.call(arguments)); | |
| }; | |
| })(m, ctor.__on__[m]); | |
| } | |
| } | |
| } |
Author
Author
これでいい気がしてきた
export class Class {
constructor() {
for (var prop in this) {
var fn = this[prop];
if (typeof fn === 'function') {
((prop:string, fn:Function)=> {
this[prop] = ()=> {
return fn.apply(this, Array.prototype.slice.call(arguments));
};
})(prop, fn);
}
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/12756423/is-there-an-alias-for-this-in-typescript