Created
May 6, 2024 18:11
-
-
Save osbre/eda54fbe73ba7676c83f616b3d5bf288 to your computer and use it in GitHub Desktop.
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
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