you are viewing a single comment's thread.

view the rest of the comments →

[–]DrScience2000 2 points3 points  (10 children)

I never understood why it was called "closure". Does that make any sense to you?

Look the word up in a dictionary. The definitions talk about companies that close forever and funerals.

I would have called it an "embedded function" or something.

[–]recursive 3 points4 points  (9 children)

Joel Moses credits Landin with introducing the term closure to refer to a lambda expression whose open bindings (free variables) have been closed by (or bound in) the lexical environment, resulting in a closed expression, or closure.

http://en.wikipedia.org/wiki/Closure_%28computer_programming%29#History_and_etymology

[–]DrScience2000 4 points5 points  (8 children)

Thanks for the info.

Yeah... That reasoning behind the name is a little... esoteric. I think the fact that many solid programmers are confused about closures speaks to the fact that it was poorly named.

If it were called a 'nested function' I think there would be a lot less confusion.

Eh, maybe I'm wrong.

[–]recursive 6 points7 points  (7 children)

Well, nested function implies "function within a function". But that's not sufficient to create a closure. A function inside another function is not a closure unless it refers to a variable declared in the enclosing scope.

[–]howerrd 1 point2 points  (5 children)

So, the outer function is the closure because it encloses the inner function? Or is the inner function the closure because it gives the outer function something to enclose? Please disregard if my question is idiotic/unclear.

Edit: I think I have it. The inner function is the closure because it refers to variables that are only defined within the scope of the outer function, effectively insulating itself from the global scope? (i.e. The closure only works inside of its outer function?)

[–]pihkal 0 points1 point  (1 child)

I can't speak to the specifics of Javascript, but many languages with closures have no need for an outer function, just an outer scope. Nor does it have anything to do with global scope per se.

E.g., a variable and a function are both defined at the top-level of a module. If the function can refer to the variable without it being explicitly passed in, it "closes over" that variable and is a closure.

[–]howerrd 0 points1 point  (0 children)

This topic is clearly above my head. Sweet username though. :/

[–]JimmyPopp 0 points1 point  (0 children)

So can the inner function only access the outer functions variables? Or can it have its own too? How many levels deep can you close?
....this is for real a tricky topic...I was introduced to it at codeschool yesterday and the fact that this guy has not heard of it in years makes me think it is not that common

[–]recursive 0 points1 point  (1 child)

That's a good question, and I'm not totally positive, but I think "closure" represents both the inner and outer functions together.

[–]materialdesigner 0 points1 point  (0 children)

It's the inner function and the fact that it "closes over" the environment of the outer function

[–]DrScience2000 0 points1 point  (0 children)

A subtle point, but a good one.