4 ms·
> In short, a closure is like a semi-permeable box, letting information from the outside get in but never leaking anything out. This is just wrong. For example
by iooi 6y ago
> In short, a closure is like a semi-permeable box, letting information from the outside get in but never leaking anything out.
This is just wrong. For example:
const counter = () => { let count = 1; return () => count++ }
- uryga 6y agothe counter's value isn't leaking out, you're explicitly returning it. that's the point – the only thing you can "get out of" a closure is what it returns when you call it (see also: the JS pattern of using closures to create objects with truly private "fields")
- NegativeLatency 6y agoYou could mutate some shared/global state from inside a closure as a way of getting information/state out
- obedm 6y ago(author here) this is true! That's what I'm trying to explain in the `state` section of the article. State lives in a "global" and the component is modifying it. So it's "leaking info out". It's hard to make comprehensive mental models that cover all the bases and leaves nothing out
- uryga 6y agoi considered adding "mutate a global" in there but didn't feel like that was relevant to @iooi's comment. maybe i should've quantified "the only thing" a bit more :)