DEV Community

Discussion on: Closure? I Hardly Know Her!

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

There's no reason why global functions with global variables can't be considered closures as well. They wrap up their state in the function. This is particularly true when exporting functions from modules.

In Leaf I consider closures and instance functions to be basically the same. A closure is merely a class instance with an implicit this. Under the hood there's not much difference at all.

Collapse
 
rpalo profile image
Ryan Palo

Interesting. I never thought about it like that before. That’s some great under-the-hood knowledge to have. Although instantiating a closure’s state by importing a module (if that state is intended to be mutable) makes my programmy danger sense tingle a little bit.