{"id":13807,"date":"2011-08-13T10:21:06","date_gmt":"2011-08-13T17:21:06","guid":{"rendered":"http:\/\/www.kith.org\/journals\/jed\/2011\/08\/13\/13807.html"},"modified":"2011-08-13T10:21:06","modified_gmt":"2011-08-13T17:21:06","slug":"learn-about-closures","status":"publish","type":"post","link":"https:\/\/www.kith.org\/jed\/2011\/08\/13\/learn-about-closures\/","title":{"rendered":"Learn about closures"},"content":{"rendered":"\n<p>I encountered the computer science idea of a &ldquo;closure&rdquo; 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.<\/p>\n<p>So when I came across the interactive JavaScript tutorial site &ldquo;<a href=\"http:\/\/nathansjslessons.appspot.com\/\">What's a Closure?<\/a>&rdquo; I figured it was worth trying out.<\/p>\n<p>And it sure was.<\/p>\n<p>It's a series of twelve brief lessons&mdash;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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>But even so, overall I think this is a great little tutorial.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>I encountered the computer science idea of a &ldquo;closure&rdquo; for the first time sometime in the past couple of years&#8230;.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[68],"tags":[],"class_list":["post-13807","post","type-post","status-publish","format-standard","hentry","category-programming"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/posts\/13807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/comments?post=13807"}],"version-history":[{"count":0,"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/posts\/13807\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/media?parent=13807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/categories?post=13807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kith.org\/jed\/wp-json\/wp\/v2\/tags?post=13807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}