Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Closure" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle introduces closure, a function remembering its variables no matter where it is being called, as an essential concept to understand for functional programming. Kyle then discusses how to close over parameters to preserve the purity of a function.


Transcript from the "Closure" Lesson

>> Kyle Simpson: All right, so we've already hinted at the idea of closure in this course. A couple of time, we've already used it. But I wanna be very specific about a definition for closure, cuz there's a lot misconception about this concept. And it is absolutely essential. You just cannot do functional programming without a solid understanding of closure.

So I wanna give you a very specific definition for closure that is based upon what we can observe in our programs. And that is closure is when a function remembers the variables around it even when that function is executed elsewhere. And by around it I mean any variables that you access outside the function that aren't directly passed in, they're accessed like for, remember when we accessed global variables, things like that.

Anytime you access a variable from an outer scope, and then you take that function that is doing that access, and you pass it somewhere else, and you run it in a different scope. If you thought about it logically you'd say, well, it should forget about that variable, cuz it's running in a different scope.

But it doesn't forget about the variable, and the reason it doesn't is because of closure. And if that sounds too abstract to you, let me remind you that we've already seen this several times in code we've looked at today. Here's an example, I'm illustrating the idea of closure here.

I have a makeCounter function. makeCounter doesn't take any inputs in it. It declares a variable, line 2, called counter and it returns us back a function, which every time we call it, is going to increment that counter variable. So on line 8, I make my counter, I say var c = makeCounter.

And I call c with no inputs, and I get back the value 1. And then I call c with no inputs and I get back the value 2, and then 3, and then 4. So it's in a counter, right? Could be useful in our programs to have a counter.

But let me ask you, this is definitely closure because we have the inner function increment, closing over that counter variable. It's remembering it, even though makeCounter has finished, the C function still remembers it and can still keep updating it. That's how it works. That's called closure. But I wanna ask you, is the C function a pure function call?

>> Kyle Simpson: Look at like 10, 11, and 12. Are those pure function calls?
>> Student: It can't be because it's pulling from the dom.
>> Kyle Simpson: It's not pulling from the dom.
>> Student: Or pulling information.
>> Student2: Not getting the same output.
>> Student: Yeah.
>> Kyle Simpson: We are not getting the same output every time.

We're passing in the same input, which in this case, is no input. But every time, we're getting a different output. So it absolutely violates one of the fundamental premises of a pure function. So here's the takeaway, closure is not necessarily functionally pure. But closure can absolutely be used in functional theory.

So the key is if I'm closing over a variable, this counter, it can't be reassigned. And if it's getting reassigned every time that's a big no, no because now we're not closed over a constant anymore, we're just closed over some changing state, in which case we now have an impure function.

You might have to do that in your programs but that's not gonna be a pure function goal. Are you with me? So let's look at some examples of using closure that is consistent with functional style, that is going to produce a pure function. Remember unary? That was one of the first utilities that we looked at, our first higher order function, It takes it an (fn), and remembers that (fn) and uses it later.

How does it do that? Why did the fn variable from line 1, the parameter, not get garbage collected when unary finished?
>> Kyle Simpson: The answer is because the inner function one which got returned is holding onto that parameter, via what we call closure. It still has access to that thing.

So later on whenever we call f somewhere, it still remembers the original thing that was passed into unary by way of the parameter fn. And that's not the only example we saw of closure, remember addAnother? We talked about this one, remember how it's referencing that z variable, even though that z variable was passed in and an outer scope.

The addTwo function is closed over the variable z, which is why it's able to use that later on.
>> Kyle Simpson: And the key thing here is that in the both of those slides the fn on the previous slide and the z on this slide. In both of those cases we are referencing a variable outside of ourselves, which seems like it would to be a no no, but it's safe functionally because it doesn't change.

It's a memory over a thing that doesn't get modified. That's what makes it still function pure.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now