I Need Closure

Turcan Cicek
Nov 1 · 3 min read

Before I started learning Javascript, I had a pretty limited experience with programming, and most that time was spent with Ruby. I’m not going to sugar coat my feelings about JS, it’s no Ruby; it seriously lacks that sweet syntactic sugar that I’ve grown accustomed to over the past few months.

Harsh words aside, Javascript has some really cool features that I hadn’t run across while learning Ruby, and it’s easier to make associations to the concepts that I know than it is to learn entirely new ones. One of the most interesting concepts I came across was a closures and callbacks.

Call me back, maybe

In order to iterate over an array in Ruby, we can use enumerables, which creates an enumerator object, which then yields to a block.

In order to do the same thing using JS, we can use a number of iterators, or a higher function in the Array.prototype “class” such as Array.prototype.forEach or Array.prototype.map which takes a callback function.

Callbacks are similar to blocks in Ruby, but they can be referenced rather than directly placed into a block. Let’s look at the comparison side by side:

# ruby
[].each { |element| element.methods }
// javascript
[].forEach(element => element.functions)

It’s uncommon to see the code actually pulled out from the enumerator block in Ruby, however it’s common practice with the equivalent in Javascript. As rubyists, and object-oriented programmers, we should strive to maintain a separation of concerns in our methods, by writing small, maintainable methods in easily readable segments, which “do” one thing. It has been a pleasure writing callbacks in javascript, because of how easy it is to enclose each function.

Javascript enclosures and Ruby blocks

Languages with first-class functions, such as Javascript, allow functions to be stored as variables, passed as arguments to other functions, and can even return other functions. A closure is a first-class function with its own environmental variables.

Ruby methods share few similarities, but Ruby blocks are closures. So are Procedures (procs), and Lambdas.

With Procs and Lambdas, we can store blocks into variables. They can be instantiated and called:

That lambda looks pretty familiar

Procs behave like blocks, but they can be stored in variables. Lambdas are procs that behave like methods. What’s the difference? Procs, similar to functions in JS, won’t throw an ArgumentError if too many arguments are passed into the block. Lambdas act like methods, in that they will throw an error. There are some more technical differences that aren’t in the scope of this post (there’s a closure joke in there somewhere), but I’m sure you can find plenty of literature on that.

Now that we’ve managed to store a block into a variable, let’s make that final stretch:

Check that out, we’re literally calling it

That’s not exactly anymore readable, but I suppose it conveys intent slightly better.

What if we used the Unary & operator ?

That’s more like it.

Turcan Cicek

Written by

Former radiochemist, current SWE student at Flatiron School.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade