Reputation: 765
while debugging, Where do I find closure in the case of anonymous functions?
In the case of non-anonymous function, it seems to appear under "functional scope". For eg:
function outer(x){
var temp = 0;
return function inner(){
alert(++x);
};
}
var out = outer(3);
out();
debugger;
out = outer(5);
out();
In this case, I can seeclosure_in_functionalscope in watch expression of "out".
However, when I use anonymous function as shown below:
function outer(x){
var temp = 0;
return function (){
alert(++x);
};
}
outer(3)();
debugger;
outer(5)();
In this case, I don't see anonymous function as closure in watch expression. Which object stores the closure in the case of anonymous function?
Upvotes: 3
Views: 1744
Reputation: 19229
It has nothing to do with the inner function being named or not.
In the first screenshot you're inspecting the out
variable, which references a function returned but outer
. That function has x
in its closure scope.
In the second screenshot you're inspecting the outer
variable, which references a named global function. In that code snippet you don't have any variable to reference the result or outer(3)
. If you assign that to a variable just like you do in the first example var out = outer(3)
and put a breakpoint after that assignment, you'll be able to see out
's closure scope. Alternatively, you can inspect that by adding a "watch expression" of outer(3)
in the debugger without the need to modify your code.
Upvotes: 3