Skip to content

Instantly share code, notes, and snippets.

@izhangzhihao
Last active June 5, 2020 06:46
Show Gist options
  • Save izhangzhihao/b8e023121bd0c6e5af3b1abe31b48bca to your computer and use it in GitHub Desktop.
Save izhangzhihao/b8e023121bd0c6e5af3b1abe31b48bca to your computer and use it in GitHub Desktop.
Lazy implementation in typescript
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