Closures Workbook: A Tutorial on Functional Programming in Javascript

CS559 Function Programming (Closures) Workbook

This is a review of some functional programming concepts in JavaScript for CS559.

Functional programming concepts (like closures) are extremely useful for graphics and user interface programming. The good news is that they are well supported in JavaScript. However, they are tricky to learn: students often need to see lots of examples before they make sense.

This workbook provides a quick tutorial that reviews the functional programming concepts in JavaScript. It tries to use examples that make it clear why they are useful for basic web programming (since this workbook comes before we do much graphics programming, it doesn’t include graphics examples).

This workbook is optional. We will not collect it. We will not grade it. You can be pretty confident there will be questions about closures and other programming concepts on the exams.

Why is this hard to learn?

I think there are 3 things that make the functional programming concepts we use in class hard to learn at first:

  1. Some of the concepts (e.g., closures) are tricky. There is no substitute for seeing lots of examples and trying them.
  2. Many students are learning them in the context of a new programming language, so they are learning two things at once, which adds to the trickiness.
  3. The examples used in standard books don’t necessarily motivate why we want them for class, so when the need to use them in class comes up, you may not recognize it.

I do think it is worth learning these things. Functional programming is an interesting way to think about programming - even beyond Javascript and graphics. But it is particularly useful when the language supports them well (JavaScript does), and when you need to write interactive programs.

Other Resources

This workbook will not teach you everything. It’s mainly meant to provide another example.

The Javascript in CS559 page will give you resources for learning JavaScript. Some of the books are particularly good at introducing functional concepts.

I made a video Closure Tutorial that might be helpful as well. Seeing the activation records “animation” as drawings (what is happening “inside” when closures are run) may be helpful. The slides for this tutorial are part of this workbook (before-lecture slides) (post-lecture slides).

Get started on Page  1  (A Simple Example)!