Skip to content

Instantly share code, notes, and snippets.

@KMahoney
Created December 8, 2023 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KMahoney/99d4718ac897b8c42353301eb8114234 to your computer and use it in GitHub Desktop.
Save KMahoney/99d4718ac897b8c42353301eb8114234 to your computer and use it in GitHub Desktop.
This was an annoying bug to find
class Bar {
constructor(public foo: Foo) {}
}
class Foo {
private cachedBar: Bar | null = null;
getBar() {
if (this.cachedBar !== null) return this.cachedBar;
this.cachedBar = new Bar(this);
return this.cachedBar;
}
}
function makeFoo() {
const baseFoo = new Foo();
// extend with some convenient properties
return Object.create(baseFoo, { bar: { value: baseFoo.getBar() } });
}
const foo = makeFoo();
console.log(foo === foo.getBar().foo); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment