Check out a free preview of the full JavaScript: The Hard Parts, v2 course

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

Will fields questions from the audience regarding whether there can be a wrapper such that there is another execution context returning out the first closure, function decoration, and whether variables that aren't referenced by the returned function are accessible by the backpack. Clarifications are also made on a private variables, and error handling within closure.

Preview
Close

Transcript from the "Closure Q&A" Lesson

[00:00:00]
>> Will Sentance: Dan, go ahead.
>> Dan: Kind of two, the first one is if you had a secondary execution context wrapping, can you have a back pack of the second level?
>> Will Sentance: Yeah, absolutely yes. So, I would say that, go experiment, but it is a chain. This is what's called, well, we're gonna learn the name of this in a moment, but yeah, it is, absolutely, you can go play with that, for sure.

[00:00:22]
But yes, it is a chain, yeah, second question?
>> Dan: Can you have a callback that accesses the data?
>> Will Sentance: Wow, so you're saying, could we pass into here a function that, is that what you're-
>> Dan: Yeah, that would then access the variables?
>> Will Sentance: Yeah, so this is what's called function decoration.

[00:00:39]
Many of the techniques enclosed in a, sorry, in functional programming use this principle, where you would pass, one knows a to detail, I want to do like a half, sort of half-baked version of this. Go watch the functional programming Hall parts where we do this in absolute full, but we can take a function in here.

[00:00:57]
We can define a brand new function in here, so she gets a backpack. That function will then have access to the argument function passed into outer, when we return out in its backpack. I mean, we can store code in the backpack and run the function. Meaning that that returned-out function will be, we could make it behave pretty similarly to the function that got passed in, but with some changes.

[00:01:20]
It's behaving similar, because it's in the, behind the scenes, gonna be running the function from its backpack. That's a half-baked answer. But absolutely, you can literally store code in the backpack as well. And that's what enables carrying, partial application and function decoration techniques. All of them sit on this, but sit on us passing a function in which ends up in the brand new returned out function's backpack.

[00:01:43]
And that is the decoration process, different to other languages handle decoration. Decoration's where you edit a function or appear to, in reality what you're doing, brand new function that uses the decorated, the change function from his backpack. That's something that, don't worry if you're going what the hell does that mean?

[00:02:01]
But if you want to learn more about it go watch the functional programming hotpots. Could be the, be the only person. I'm sure it can be very popular. I'm sure it's gonna blow up. Excellent. All right, thank you, Dan. Great questions, Peter.
>> Peter: Very similarly, you called the counter data private within there, and then you could store a function there, and then that would be a private function.

[00:02:20]
>> Will Sentance: Yes, the term private. I gotta be careful on that because all these terms are general terms that have many competing definitions in different languages, so is it private? Well, as long as we can do certain things with it then probably not. Is it private in the sense that we're not able to suddenly randomly override to the global memory,?

[00:02:40]
Sure. So just be careful with any of these terms, they're all a little bit elusive across different languages. Yeah, go ahead [INAUDIBLE].
>> Speaker 4: There's a question from online about scope and is basically where does scope come into play?
>> Will Sentance: You'll see it in one second, absolutely, yeah. Yeah, go ahead, Jason.

[00:02:58]
>> Jason: Error propagation, we're nesting a bunch of calls, we're nesting a bunch of operations. I can still make an access error on those variables, I can still ask for an MSE in an array that doesn't exist. But now we've got a chain of references to propagate our error through.

[00:03:16]
>> Will Sentance: Yeah, I mean this is this is one of the interesting challenges is that when we all using traditional console logging, I find console log my new function. It's got a bunch of state data attached to it that I don't get to see on my console. That's gonna be pretty hard to debug.

[00:03:35]
Unless we know what's happening, of course. Now, kindly the chrome dev tools do give us the ability to pause our execution at this moment. And if we do, we'll see the contents of the backpack on the right-hand side. They give it the official name, which I'll tell you in a second.

[00:03:53]
There's one thing I did, nobody raised, but I do want to bring up. Yeah, go ahead, Mark.
>> Mark: If in outer-
>> Mark: You created another variable-
>> Will Sentance: There's the question, go ahead, Mark.
>> Mark: If incrementCounter doesn't call.
>> Will Sentance: Doesn't reference, doesn't reference.
>> Mark: Doesn't reference, does he get it?

[00:04:08]
>> Will Sentance: There it is perfect, as in question Mark is about to give. Yes, fantastic question, Mark. We'll be clear everyone got that question by Mark. Mark said hold on. But what if, in this local memory I created, let's call it, I don't know, Mark's counter and the year and this.

[00:04:26]
Mark's counter is seven, the returned out function, can I edit this returned out function? No, therefore, that, Mark's counter goes in the backpack. Can I get anything on a backpack directly? No, I can only get it by running the function. So I know exactly, or Java knows exactly the moment of returning out what could ever be accessed in that backpack.

[00:04:49]
And so in modern versions of, the implementations of JavaScript in the chrome engine and other engines as well, I think for 39 on. JavaScript optimizes, what is put into the backpack. The link is made the entire local memory, and it's just a link to where that stops. Yeah, this isn't if we declare Mark's counter inside of outer is gonna be stored, it's just when we're finished running outer, normally it will all be deleted.

[00:05:14]
Instead, anything that the function ever makes reference to when it would get run eventually, gets pulled out with the function on its back into my new function. But if that thing, Mark's counter is never referenced, never referred to, never used by the function and it gets returned out jobs, you can literally look into this function and go, does it ever reference?

[00:05:38]
Nope, no reference made to Mark's counter, only to counter. And so there's no point in being in the backpack cuz that would be what's called a memory leak. That is a say specifically, use space in the computer's memory. With a label, you've got data used with a label, where we can't ever act as that label anymore.

[00:05:56]
And so that data is completely inaccessible, as it is literally just wasting space in the computer's memory. That's what a memory leak is. And that would be the case here. That would be stored, we'd have Mark's counter is 7, and yet, if we ran myNewFunction, it never makes reference to it, and we can't get to directly so it's just waste of space.

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