Ask HN: What is the difference between a closure and an object?
Reading PG's criticism about OOP, he mentions that if he ever really needed objects, he'd use an array of closures instead. When I read this I knew little about what closures were, so intrigued I read up I them.
Now I can't figure out how they're different, except how a language might implement them.
So what is the conceptual and practical difference between the two?
4 comments
[ 2.9 ms ] story [ 17.3 ms ] threadA closure is like if you needed a forearm, you created one on-the-fly for the specific instance, and it exists as long as it's needed. You would never have the issue of having excess multiples of arms or legs because closures, although maintaining an internal state, only exist as long as they are invoked.
It's a simple example, but maybe that helps clear it up. Perhaps some people can help extend the metaphor if it proves useful.
http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent
It may also help to contemplate the techniques used to get "private variables" in vanilla Javascript.
If you treat closures like objects, they are tedious, and missing lots of nice conveniences that OO languages provide for their objects.
If you treat Objects like closures then they are heavy weight, have too much formality, and have annoying limitations to work around.
That's not a big difference, but it is a difference.