Last active
June 5, 2020 06:46
-
-
Save izhangzhihao/b8e023121bd0c6e5af3b1abe31b48bca to your computer and use it in GitHub Desktop.
Lazy implementation in typescript
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
export class Lazy<T> { | |
private instance: T | undefined = undefined; | |
private readonly initializer: () => T; | |
constructor(initializer: () => T) { | |
this.initializer = initializer; | |
} | |
public get value(): T { | |
if (this.instance === undefined) { | |
this.instance = this.initializer(); | |
} | |
return this.instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment