From 915d876890f819740c7a2b779eb0e9733a658768 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 11 Jan 2022 19:25:52 +0100 Subject: [PATCH] chore: bind functions --- src/image.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/image.ts b/src/image.ts index b39a2eb1bc7..e16fa348134 100644 --- a/src/image.ts +++ b/src/image.ts @@ -13,6 +13,14 @@ export class Image { readonly lorempicsum: typeof LoremPicsum; constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Image.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + this.lorempixel = new Lorempixel(this.faker); this.unsplash = new Unsplash(this.faker); this.lorempicsum = new LoremPicsum(this.faker);