Skip to content

Instantly share code, notes, and snippets.

@minodisk
Created March 26, 2013 05:12
Show Gist options
  • Select an option

  • Save minodisk/5243300 to your computer and use it in GitHub Desktop.

Select an option

Save minodisk/5243300 to your computer and use it in GitHub Desktop.
onや_onから始まるとthisで束縛するクラス
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]);
}
}
}
@minodisk
Copy link
Copy Markdown
Author

@minodisk
Copy link
Copy Markdown
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