Scopes and Closures In-depth 19 - Closures In Callbacks

preview_player
Показать описание

One of the prominent uses of closures (that we do and don't even recognise it) is when we use callbacks in JavaScript. Learn how closures apply there.
Рекомендации по теме
Комментарии
Автор

your way of explaining the subject is quite amazing, i love your way of teaching

mohdirshad
Автор

Got it.!
Because it's a function, for every execution of it one object will be created and gets garbage collected also, once it finishes executing it. So there will be total 5 fn(s) created by the script in question :)
Thanks man..! You did a fabulous job..!
It'd great if you do the same kinda tutorial series for Node.js.
Thanks, once again..!

chetanchoulwar
Автор

Hi Koushik,

So I notice that if I increment a (a++) after the set time out function, what actually prints on the screen is 11. I understand why it would do this as it is pointing at the above a. However, I have a couple questions. How does this help with Callbacks? Also, is there a way to increment a(which is in global scope) without it actually changing the value for a used from inside the set timeout function. Like say a b declared inside the anonymous function will have its own copy..so is there a way for global variables to have their original values used inside the set timeout function and not change during the time delay.

Also, NodeJS uses a lot of callbacks, whats going on there?

Thank you!

sankalp
Автор

Does every function has information about all variables in scope, which that function was declared?

hubertkocz
Автор

great vid
any idea how can we exercise everything ? thank you !

orz
Автор

what about this ?

var a = 3;
var fn = function(){
console.log(a);
console.log(b);
}
setTimeout(fn, 1000);
var b = 4;

according to this video, a = 3 and b = undefined at the time when fn is created. but it is printing as a = 3 and b = 4 why ?

mukundkumar
Автор

Hi, Instead of word "snaphsot" being passed to the remote function, is it appropriate to say "a reference is passed"?

vicky
Автор

One question.
So how is actually fn executed in setTimeout if we don't put () for invocations? Where is the invocation for that function, we just send body of the function, but where do we invoke this function ? :)
Also, fantastic series of videos, i look all for one day, just fantastic, i learn some new things. Thanks

vladanng
Автор

according to me above in not closure its just the scope because
setTimeout will be in global scope and will have its own scope while executing.It will check if it contains variable 'a' in its scope if not then check in one level above scope that is global it finds there and prints the value.Please help me understand here how it is closure.I think it would be closure if the declarion of variable was in fn function

mirzafarrukh
Автор

*Example showing execution sequence...*

var a = 0;
var b = 0;
var c = 0;

var printVars = function(){
a++;
console.log("printVars call #" + a);
console.log("a= " + a);
console.log("b= " + b);
console.log("c= " + c);
}

var incrB = function(){
b++;
}

setTimeout(printVars, 1000);
c++;
setTimeout(incrB, 3000);
setTimeout(printVars, 5000);

console.log("End of Script");
console.log("a= " + a);
console.log("b= " + b);
console.log("c= " + c);

/* OUTPUT:
End of Script
a= 0
b= 0
c= 1
printVars call #1
a= 1
b= 0
c= 1
printVars call #2
a= 2
b= 1
c= 1
*/

devexpost
Автор

I'm imtpressed with your teaching skills - it's rare in youtube programming tutorial world. But here you are not completely clear. The snapshot that's created is only of memory addresses of the variables, not their values. It easy to confirm by increasing "a" variable right after setting the timeout. The value printed on the console will be 11.

probablyjustme