« Items: Women, tech, and tech companies | Main | Finished watching Caprica »

Learn about closures

| No Comments

I encountered the computer science idea of a “closure” for the first time sometime in the past couple of years. I understood that it involved treating a function as a value (so that, for example, you can assign a function to a variable), but I wasn't entirely clear on what else was involved.

So when I came across the interactive JavaScript tutorial site “What's a Closure?” I figured it was worth trying out.

And it sure was.

It's a series of twelve brief lessons—you can probably go through the whole thing in an hour and a half. On each page, there's an explanation, and a code example, and then you're asked to write your own code (right there on the page!) that does something similar, and when you're ready, you click a button, and the system runs your code and checks to make sure it works, and tells you whether you got it right or not.

The first couple of pages may be understandable by people with little or no programming experience. But it starts getting into some relatively advanced stuff around page 4 or 5. If you don't already understand the concept of scope, you may have a hard time getting past those pages. There's a discussion of scope on page 7, but not, I think, one that will be clear to beginners.

Also, the error messages if you get something wrong are not very beginner-friendly; they use JSLint to check the syntax of your code, and then they run a series of tests to see if your code gives the right output, but the error messages if the tests fail aren't very beginner-friendly.

So this isn't a tutorial for people new to programming, unfortunately. But if you understand the basics of programming in any modern language with C-like syntax, I think you should be able to get through these tutorials.

Page 9, on stateful closures, amazed me. I sorta kinda knew you could do that kind of thing in JS, but I don't think I ever really got it before. Wow.

Page 10, on private data, took me a while to really get; there's no explanation of the rather unexpected syntax, so I got a little lost in the nesting of functions. But eventually I figured it out.

Page 11 took me quite a while. Page 12's explanation of continuation-passing style is kinda confusing; I think if I hadn't already had some exposure to CPS, I wouldn't have understood it, and even after completing it, I'm not really sure I understand it.

But even so, overall I think this is a great little tutorial.

Post a comment