1. 13
  1. 5


    var x, y, z;

    (function() { return x + y + z; })() // closure, because it “encloses” the surrounding scope, capturing the variables nearby.


    I think the site actually really makes understanding a closure more complicated. My 4 line example, most people would understand it in a minute.

    1. 5

      Somewhat. Pascal can do what you did. A better example is returning an inner function for use later. That makes clear that the variables out of scope of the inner function are stored somewhere to be used later.

      1. 3

        I think understanding closures is complicated by the fact that C and top-level Pascal functions are closures: they capture the global variables, if you take a pointer to the function and call it then it will still reference the globals. When you learn a language like C, you learn about closures but with the seemingly arbitrary restriction that they’re allowed to capture state only one level deep. If you understand how these languages are implemented, there’s some purpose to this as a simplification (though when you get to dynamic linkage, the implementations get a lot closer to a classical closure: you typically have a global-pointer register that describes the environment and so you could easily capture a different environment to the standard global one) but at an abstract level you just have a set of limitations on when you’re allowed to declare a closure.

        1. 1

          I, for one, would like to see an example of a C program with dynamic linkage (like you get on Linux) where you can have two different global environments, because I don’t think of C functions (or Pascal functions) as closures.

          1. 1

            The environment isn’t really set up for it, so you’d need to write a custom runtime linker. It also depends a bit on the ABI. For example, consider this trivial C function:

            int x;
            int getX(void)
                    return x;

            This is a closure that captures x. When you compile this with -fPIC for x86-64, you get something like this:

                    movq    x@GOTPCREL(%rip), %rax
                    movl    (%rax), %eax

            There is a global offset table (GOT) for this library, which contains a pointer to x. The GOT is effectively a table of pointers to every captured global for everything bound to this library. The @GOTPCREL in the first instruction directs the linker to fill in the immediate offset in this instruction with offset of the GOT. That’s pretty difficult to interpose on (though the GOT will be on a different page to the code, so you could potentially do tricks with virtual memory to give different views of it. On some architectures, the default ABI reserves a global-pointer register to hold the base of the GOT and resets this in PLT stubs so that functions don’t need to compute it. On these systems, it’s possible to simply modify the $gp register to bind a function to a different view of globals (as long as you correctly set up the GOT).

            Common linkage means that shared libraries and the main program may both define a global. If the program doesn’t define it, the version in the shared library is used, but if both define it then the version in the program should be used. As a result, the binding between code and references to globals needs to be dynamic. You end up effectively implementing a closure mechanism, though one that’s optimised for the case where you only ever bind a function to a single environment.

        2. 1

          I agree, that is an important aspect!

        3. 3

          The article contains a tl;dr with a simple example (similar to yours), pithy definition and a disclaimer that the rest of the article takes a lateral approach to explaining a closure.

          One particularly illuminating part is the etymology, of which I wasn’t previously aware, and which your example obscures.

          Arguably the more important part is understanding the implications of closures, given that they’re one of the more (if not the most) frequent source of memory leaks in JavaScript.

        4. 5

          the cutesy foul language seems really unprofessional

          maybe i’m getting old, but always feel like if a vulgar word needs to be used so prominently in a site, perhaps there’s a lack of vocabulary on account of the writer

          like if you have a curse free mirror, then what’s the point …or maybe they’re just being edgy

          1. 8

            I’m a bit confused - I honestly only skimmed the article, but the only profanity is the domain name? I’m usually against vulgarity (it often hides lack of rigor by adding effect), but as the project name is a standing phrase, I don’t see that much of an issue with it.

            1. 8

              While I understand that many hate javascript, to call it a foul language seems a bit over-the-top, don’t you think?

              1. 2

                more upvotes for you, sir

              2. 6

                In my experience I’ve seen no correlation between use of profanity and access to vocabulary, yet it’s a meme I’ve heard my whole life. As far as I can tell the meme survives because it has truthiness for those who don’t like profanity; also because the converse inference is frequently propagated in fictional media.

                To me, “whatthefuck.is” reads rather similar to “…for dummies”.

                1. 1

                  I’m late to reply here, but I do actually have experience with people that were outspoken about that their profanity was used for lack of sharper tools to express strong disapproval. Particularly, I had conversations with two co-maintainers about that. Both were 2nd and 3rd language English speakers. I have to say though: both were happy with a little bit of language coaching in the back, and got more effective at communicating in the process.

                  In general, I find the discussion around language expectations rather poor from the side of first language speakers.

                  If profanity is purely used as a stand-in for the inability to express better or express a good argument, it’s also to be noted that similar patterns exist without the use of profanity. For example, stand-ins can be: language hating (“because everyone knows PHP does it wrong”), memes (“here’s a WAT, instead of explaining what’s wrong with this code to everyone in the room”) or filling a text/talk with a lot of humorous fluff (“I don’t have many points here, but I’d like to fill more space”).

                  This does not look like this, though - such a domain name is consciously chosen and I agree with your reading as “for dummies”.

                  1. 2

                    I’m even later to reply, so all good. :-)

                    It’s likely that both our experiences are valid given the different contexts. My experience is that people with deep vocabularies are just as happy to throw an f-bomb as anyone else so long as they feel it socially acceptable to do so. You may be right that people with deep vocabularies are less likely to be in such a position though.

                2. 2

                  I agree the title is distracting when put in a list of stories, but I didn’t find it bothersome since it’s part of the concept (demystifying jargon), and it’s more tongue-in-cheek than actual indignation.

                  (FWIW if the curse-free version was there when I read it, I would have probably chosen that instead.)

                3. 2


                  Operationally, a closure is a record storing a function[a] together with an environment.[1] The environment is a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created.[b] Unlike a plain function, a closure allows the function to access those captured variables through the closure’s copies of their values or references, even when the function is invoked outside their scope.

                  This article:

                  You have a closure when a function accesses variables defined outside of it.

                  I know there was probably a meeting where we decided wikipedia is no longer for the average person. I know it is probably my fault I missed it. Still, can we revisit the decision?

                  I have tried looking up what a closure was in the past. I gave up pretty quickly thinking it must be something very specialized that I don’t need to know. Turns out its just a fancy word for something very common.

                  1. 2

                    There are whole swathes of Wikipedia articles that would benefit from some editing for clarity; the more specialized, the more idiosyncratic and jargon-burdened these articles tend to be (at least judging from my personal experience with color spaces, curves, interpolation, etc.). On the other hand, Wikipedia has to work with external sources, and is not conductive to the kind of personal interpretations you’d see in technical essays.