Transcript from the "Q&A: Garbage Collection" Lesson
>> Will: There you go, so there's more stuff to add. Now, we are now, I think, reaching most of the key pieces being added. Do we still have some medium thumbs? Andrea, go ahead.
>> Andrea: So how do you properly garbage collect the live data? Does it just, as long as there is a minding function it stays there, right?
[00:00:19] I guess I'm a little.
>> Will: So the reason you're doing this is because you want this. You mean this live data here?
>> Andrea: Yeah [CROSSTALK].
>> Will: But the reason you're doing this is you want that data to stick around.
>> Andrea: Right, so let's say you're done, let's say you're doing a big program and you don't want it to have a memory link.
>> Will: Spot on, very, very fair question. Well, where is this scope property attached to?
>> Andrea: To the function.
>> Will: To the function value.
>> Andrea: Value, okay.
>> Will: So what could I do to change out the value attached to my new function? Java script is a dynamic language, what could I do?
>> Andrea: Assign it something else?
>> Will: Assign it something else. Overwrite everything, including the hidden scope property included into the map pack. Great question, Andrea. What a fantastic question, never had that question before. Katie?
>> Katie: [LAUGH] Now I have another one so. [CROSSTALK] Okay so what if we do want, I know that we don't have access to that counter but then what is the point?
>> Will: Well we do have access, where do we have access to it from? By running this function and making sure that the function was written in such a way that it refers to something that ain't defined. So if we look, so let's see this. What is my new function, the functionality of?
>> Katie: The increment.
>> Will: Increment counter. Do we define anything inside increment counter, now known as my new function? No we don't, right? All we do is refer to this counter thing. Well that means we go looking for it in local. We didn't define anything in local, right, so we look next up in the backpack that came out on the my new function.
[00:01:56] On the increment counter function stored in my new function. Had a little backpack on it where counter is zero in it and so we can go and interact and increment counter to one. Katie?
>> Katie: What if I want to use that outside [CROSSTALK]
>> Will: It's sort of bundled up inside there, right?
>> Katie: Yeah.
>> Will: Well then let's write a really useful statement. Let's change the definition of increment counter to counter plus-plus, sure, but then add a line in it that says, return counter. So now, if I ran my new function, counter plus-plus, do I find counter in my local memory?
[00:02:37] No? Where do I look next, Muhammad?
>> Muhammad: In the backpack.
>> Will: In the backpack. See, tell me that isn't the best term for it.
>> Will: In the backpack what do I find? Well, let's pretend we haven't run it yet, what do I find, Mohammad?
>> Muhammad: Zero.
>> Will: Zero, yeah, counter is zero increment to one, and then the next line I hit inside the myNewFunction call says return?
>> Muhammad: Counter.
>> Will: Where do I find, where do I look for counter first?
>> Muhammad: Local.
>> Will: Do I find it?
>> Muhammad: Yeah. Well no, I guess. It's still gonna [INAUDIBLE]
>> Will: No, I go up here, what do I find? One, and so what do I return out? One, and the one returns out. Okay, do you see, now I'm putting it back into my sort of regular land. So I just write this function in a smart way. I can also make my increment counter function, not an increment counter function but just a increase by.
[00:03:18] And pass in a num to the definition. Which, then when it gets called, could have a 3 and make counter++ actually be counter += 3, right? And then that's gonna actually update this. So I can really interact with it any way I want. I just gotta write my function, the internal function, before I pass it out, I need to write it in a way that makes it interactive.
[00:03:41] Makes sense? Great question Katie. Go ahead, Griffin.
>> Griffin: Could counter be like an array or an object?
>> Will: It could be anything you want. And we're gonna see, that's what allows us to do some of the most powerful things with this. Spot on, Griffin. There was still another clarification, yeah Andrew?
>> Andrew: So does it take everything declared before function. Everything?
>> Will: No the bond made at the function definition, properly square bracket squared, is so the whole local memory which is not an object, but is a single entity. So the bond is not to the stuff that came above, it's to the whole set of stuff.
[00:04:19] So any set of live data, the whole variable environment as a whole, the whole thing. So, if you were to define something after, no problem. That's all still gonna be bonded in there as long as when you return that increment counter, that that increment counter functionality makes reference to all that stuff.
[00:04:36] If it doesn't, Jarvis is really smart and won't return the stuff that never gets referenced again in the backpack.