Created
December 25, 2021 21:07
-
-
Save SealtielFreak/c6e2233c654b66d7fa464ecb989823e6 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
interface ObjectCallable { | |
call(...args: any[]): void | |
} | |
class ClosurePrintLine implements ObjectCallable { | |
private _it: number | |
private _step: number | |
constructor(step: number = 16) { | |
this._it = 0 | |
this._step = step | |
} | |
get it(): number { | |
return this._it | |
} | |
get step(): number { | |
return this._step | |
} | |
reset(): void { | |
this._it = 0 | |
} | |
call(...args: string[]) { | |
for(let arg of args) { | |
console.log(arg) | |
this._it++ | |
} | |
} | |
} | |
/** @noSelf **/ | |
function closureCallableGenerator<T extends ObjectCallable>(obj: T) { | |
/** @noSelf **/ | |
return (...args: any[]) => { | |
obj.call(...args) | |
} | |
} | |
const obj = new ClosurePrintLine() | |
let closure = closureCallableGenerator(obj) | |
closure(`Step: ${obj.it}`) | |
closure(`Step: ${obj.it}`) | |
closure(`Step: ${obj.it}`) | |
closure(`Step: ${obj.it}`) | |
obj.reset() | |
closure(`Step: ${obj.it}`) | |
closure(`Step: ${obj.it}`) | |
closure(`Step: ${obj.it}`) | |
closure(`Step: ${obj.it}`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment