Lesson Description

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

Will explains the origins of the term "closure" and reviews the behavior of the execution context and closure reference.

Preview

Transcript from the "Closure Scope" Lesson

[00:00:00]
>> Will Sentance: So, first let's talk about how is this hidden link stored. Well, it is stored as a hidden property on this function. It's stored as a hidden link on this function, and we're going to see many more of these hidden links, but this one, as with many of the hidden links in JavaScript, is square bracket, square bracket, which is our way that JavaScript spec refers to its hidden links, square bracket, square bracket, scope, square bracket, square bracket.

[00:00:33]
And that word scope makes sense here. Scope is nothing profound besides, at any given line of our code, what data do I have available to me? So in this line of code here, inside of the execution context, the data available is here. When I return out the add one function and store it in new funk and run new funk, there is further available data on the new funks hidden scope stored data. It is stored on this hidden property scope.

[00:01:16]
So, with that in mind, what are our fancy names for this backpack? Well, one is based on another name for our local memory. Another name for our local memory is the variable environment, the around us at that moment, the environments and stuff around me, the variables or the data, old school word variable, but the data around me at that moment, the variable environment. And then, that variable environment, that data, that stored environment of data, that stored data around me at that moment is, I don't know, closed over, like a lid is closed on it and it's pulled out.

[00:02:00]
The backpack zip is closed, I don't know. Anyway, they call it, if you want to sound fancy, they call it the closed over variable environment, the closed over variable environment. The cove, okay, the closed over variable environment, I like it, but I want to be even more fancy. Okay, the even fancier term is, okay, so it's data that is referenced by, okay, it's data, that is persistent. So it's persistent data that is referenced by the scope property, so it is persistent, scope referenced data, what type of scope?

[00:02:55]
Lexically, lexical scoped, lexical scope reference data, persistent, sticks around, lexical scoped, or lexically scoped, referenced by the scope property, data. I'm not saying, by the way, these are like fundamental names for it, but these are the, you know, very precise names, lexical scoping being the rule for what data is available to us at any given moment is based on where the function was saved.

[00:03:23]
Lexical means in order of words on the page. It's like you saved add one inside of the call to outer, therefore it has the surrounding data from that saving moment. That is lexical scoping as opposed to dynamic scoping, which is where I call my function, determines the only data available to me, from where I ran it, not from where I saved it. Instead, if I ran new funk, I'd only have the data inside there or inside its core and in global.

[00:03:54]
That would be dynamic scoping. Lexical or static scoping, those are the two terms for it. Mean that our function when it gets run, has access to data from when it was saved, its position on the page in the code where it was saved. Lexical scoping. Persistent lexically scope reference data, persistent lexical scope reference data, closed over variable environment, it gets the lid gets closed on it and we pull it out.

[00:04:26]
But these are not the terms people tend to use for this backpack. You can see why I prefer the term backpack. But there is a more colloquial name for it. Does everyone know the colloquial name for this backpack? What do you think it is, Matt? Closure. Closure, Matt is spot on, it's closure. The reason I don't love that term, although in the end you get used to anything, right? I mean, I don't know, that's a, but that's a profound, inadvertently profound statement, but the reason I don't like the term closure for it is, we talk about the overall concept as closure, people talk about storing stuff in the closure, they talk about the reference as the closure, they say, I don't know, like, it's too overloaded a term, so you can absolutely and will use the term, put it in the closure.

[00:05:21]
You absolutely can and will say, let's make sure that this function, let's use closure to enable our function to have a persistent memory. But you can see even there, it's like kind of the concept, let's use the notion of a lexical scope language to ensure that our function has a persistent memory. Shorthand, let's use closure, but you can see how that term, I think, ends up being overloaded.

Learn Straight from the Experts Who Shape the Modern Web

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