I have a problem with understanding closure when I have 3 level of scopes
https://jsfiddle.net/Ar2zee/wLy8rkyL/1/
How I can get access to parameter "g" in level3 function,
var a = 10;
function level1(b) {
var c = 1;
function level2(f) {
var d = 2;
function level3(g) {
return a + b + c + d + f + g;
}
return level3()
}
return level2;
}
var temp = level1(10);
var temp2 = temp(10);
var temp3 = temp2(10);
console.log(temp3(10)); // or level(10)(); without variable
Thank you !
Line:1 invokes level1 fn and get back fn level2 stored in alias temp.
Line:2 invokes temp fn and get back fn level3 stored in alias temp2.
Line:3 now when invoking temp2 fn you execute fn level3 getting back the result of addition operation.
So temp3 is not a function but a value.
var a = 10;
function level1(b) {
var c = 1;
function level2(f) {
var d = 2;
function level3(g) {
return a + b + c + d + f + g;
}
return level3;
}
return level2;
}
var temp = level1(10);
var temp2 = temp(10);
var temp3 = temp2(10);
console.log(temp3); // or level(10)(); without variable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With