Skip to content

Instantly share code, notes, and snippets.

@osbre
Created May 6, 2024 18:11
Show Gist options
  • Save osbre/eda54fbe73ba7676c83f616b3d5bf288 to your computer and use it in GitHub Desktop.
Save osbre/eda54fbe73ba7676c83f616b3d5bf288 to your computer and use it in GitHub Desktop.
class StreamPot {
num = 0;
doIt() {
this.num += 42;
return this;
}
}
const streamPotFactory = () => new Proxy(new StreamPot, {
get: (target, prop, receiver) => {
if (typeof target[prop] === 'function') {
const instance = new StreamPot();
return (instance)[prop].bind(instance)
}
}
});
const ourStreamPotInstance = streamPotFactory()
const a = ourStreamPotInstance.doIt()
const b = ourStreamPotInstance.doIt().doIt()
console.log(a.num)
console.log(b.num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment