author class Handler { private private static _pool : Array < Handler > = [ ] ; private private static _gid : number = 1 ; /** 执行域(this)。*/ public caller : any ; /** 处理方法。*/ public method : Function ; /** 参数。*/ public args : Array < any > ; /** 表示是否只执行一次。如果为true，回调后执行recover()进行回收，回收后会被再利用，默认为false 。*/ public once : Boolean = false ; private protected _id : number = 0 ; param caller param method param args param once public constructor ( caller = null , method = null , args = null , once = false ) { this . setTo ( caller , method , args , once ) ; } param caller param method param args param once return public setTo ( caller , method , args , once ) { this . _id = Handler . _gid ++ ; this . caller = caller ; this . method = method ; this . args = args ; this . once = once ; return this ; } /**
     * 执行处理器。
     */ public run ( ) { if ( this . method == null ) return null ; var id = this . _id ; var result = this . method . apply ( this . caller , this . args ) ; this . _id === id && this . once && this . recover ( ) ; return result ; } param data public runWith ( data ) { if ( this . method == null ) return null ; var id = this . _id ; if ( data == null ) var result = this . method . apply ( this . caller , this . args ) ; else if ( ! this . args && ! ( data instanceof Array ) ) result = this . method . call ( this . caller , data ) ; else if ( this . args ) result = this . method . apply ( this . caller , this . args . concat ( data ) ) ; else result = this . method . apply ( this . caller , data ) ; this . _id === id && this . once && this . recover ( ) ; return result ; } /**
     * 清理对象引用。
     */ public clear ( ) { this . caller = null ; this . method = null ; this . args = null ; this . once = false ; return this ; } /**
     * 清理并回收到 Handler 对象池内。
     */ public recover ( ) { if ( this . _id > 0 ) { this . _id = 0 ; Handler . _pool . push ( this . clear ( ) ) ; } } param caller param method param args param once return public static create ( caller , method , args = null , once = false ) { if ( this . _pool . length ) return this . _pool . pop ( ) . setTo ( caller , method , args , once ) ; return new Handler ( caller , method , args , once ) ; } }