To my limited knowledge what you explained is lexical scope which is part of closure but closure is little more complex than that, maybe a reference of Dave Gray's video will help a little
I think the addition to lexical scope here is the fact that the function isn’t called immediately. It’s called at some later time and still has access to the variables. Lexical scope would more specifically refer to the case where you are accessing the data right then (not at a later point) I believe.
Yes you're right, you also need to understand the concept of the call stack and function execution contexts and how these two are *determined dynamically* by how the function is *called* at runtime, as opposed to how lexical scope is *determined statically* by where the function is *defined* (in the code) at compile time. This is important as you'll often see popular articles mix up the terms, execution context and scope, which will get really confusing as you try to understand how it all works under the hood of the JavaScript engine/interpreter.
Thought it was more about if you keep a reference to a function, and that function has a reference to something that is out of scope of the first reference, the second reference is available as long as the first reference still exists?
Yeah I mean that's basically the same thing here. Any time something is referenced inside of a function that isn't called until later, the fact that it keeps the reference is it.
I use that pattern all the time. I have a web app which uses handlebars for the frontend, and there are many pages where there's a list of items and I need to add event listeners to each one - so this comes in handy
Simple 👏Worth noting that if you instanciate a "const n = Math.random()" each button will have its own different "n", but always the same "n" for every click on a singular button
Hey James, what theme, font are you using? Also, did you do any changes in the default theme you used? Thanks James, keep up the good work. Hoping to watch your next video soon
@@JamesQQuick In a foreach loop, in each iteration, the index is accessed from the inner function simply by the rules of scope. Function scope - meaning everything inside has access to it. Closure: an inner function always has access to the variables and parameters of its outer function (Yes), and the mind blowing fact is when it's in a situation after the outer function has already returned. But the values are remembered to be accessed after the outer fn return. The magic point of it. ) So the idea of private variables comes from this magic feature. I've seen examples of it where it was difficult to wrap my mind around it. Sorry for the confusion. It's true what the video described. But just there are deeper meanings I believe.
your explanation is a bit cryptic. I use closures a lot and I can somehow figure out what you are trying to say, but your tut lacks enough explanation to be able to grasp it the way it is explained. Perhaps you are using it in a way I have never used it, so pardon my ignorance in that case, but I am still puzzled.
To my limited knowledge what you explained is lexical scope which is part of closure but closure is little more complex than that, maybe a reference of Dave Gray's video will help a little
I think the addition to lexical scope here is the fact that the function isn’t called immediately. It’s called at some later time and still has access to the variables. Lexical scope would more specifically refer to the case where you are accessing the data right then (not at a later point) I believe.
The best usage of closures is to create private variables I think.
@@rafalka7084 encapsulation would be the better term but yeah
Yes you're right, you also need to understand the concept of the call stack and function execution contexts and how these two are *determined dynamically* by how the function is *called* at runtime, as opposed to how lexical scope is *determined statically* by where the function is *defined* (in the code) at compile time. This is important as you'll often see popular articles mix up the terms, execution context and scope, which will get really confusing as you try to understand how it all works under the hood of the JavaScript engine/interpreter.
Thought it was more about if you keep a reference to a function, and that function has a reference to something that is out of scope of the first reference, the second reference is available as long as the first reference still exists?
Yeah I mean that's basically the same thing here. Any time something is referenced inside of a function that isn't called until later, the fact that it keeps the reference is it.
Great Video ❤❤❤
Straight to the point, cheers mate
I use that pattern all the time. I have a web app which uses handlebars for the frontend, and there are many pages where there's a list of items and I need to add event listeners to each one - so this comes in handy
Simple 👏Worth noting that if you instanciate a "const n = Math.random()" each button will have its own different "n", but always the same "n" for every click on a singular button
If it’s inside of the for each yes you are correct!
Can any one tell what theme is he using? Does he share his settings.json?
The video was amazing thank you so much. Plus, I really love your theme and your font, I do love to know and use it thank you so much.
Does anyone know what VS Code theme this is? I don’t believe this is the regular Cobalt 2 that he normally uses.
Found it - Midnight Synth
Hey James, what theme, font are you using? Also, did you do any changes in the default theme you used? Thanks James, keep up the good work. Hoping to watch your next video soon
I was scrolling the comment to see who has asked the theme question. It's beautiful. James, I need it too. Thanks for your videos.
I use the font Cascadia Code.
I’m using VS Code with the Cobalt 2 Theme.
It's in the Advent Page he mentioned, worth checking.
I think I found it - Midnight Synth
I don't understand how the i parameter in the forEach was actually logged out since you didn't define it in the addEventListener.
Great video and explanation.
That was solid 5 minutes.. thank u sir
what is this vs code theme
Thank you James.
That's it !!...loved it !
Glad you liked it!
Thanks, great video!
Love your videos, Thanks so much ❤
This is awesome 💚
Glad you enjoyed it!
But why does "i" increment?
I've been using closures for years without even knowing that's what they were. lol.
good
Your videos are awesome as your vs code theme, can you share the theme plz🙈
I use closures but not very frequent. Generally, your guide is good. Keep going.
Vs code theme please ...
I use the font Cascadia Code.
I’m using VS Code with the Cobalt 2 Theme.
It's in the Advent Page
I think this is one of the best explanations of closures I have come across.
WOw, so glad to hear that!
thanks
sry, but is it fullhd or no?
I have a vague understanding of closures, but this example does not explain closures at all in a way it is commonly used.
Hmm anything you’d like to add or clarify?
@@JamesQQuick In a foreach loop, in each iteration, the index is accessed from the inner function simply by the rules of scope. Function scope - meaning everything inside has access to it. Closure: an inner function always has access to the variables and parameters of its outer function (Yes), and the mind blowing fact is when it's in a situation after the outer function has already returned. But the values are remembered to be accessed after the outer fn return. The magic point of it. ) So the idea of private variables comes from this magic feature. I've seen examples of it where it was difficult to wrap my mind around it. Sorry for the confusion. It's true what the video described. But just there are deeper meanings I believe.
@@lexsemenenko I appreciate the detailed response. Agreed there are deeper implications and use cases than what's covered in the quick video
your explanation is a bit cryptic. I use closures a lot and I can somehow figure out what you are trying to say, but your tut lacks enough explanation to be able to grasp it the way it is explained. Perhaps you are using it in a way I have never used it, so pardon my ignorance in that case, but I am still puzzled.
3:13 wtf is a callback listener, try use simple language in a tutorial
in 2x speed for 2.5min
That was sooooo simple wow thanks a lot
Glad that helped!!