When I try to debug the javascript code which has a lot of closures I use to put a breakpoints.
Then I go to see the stack but most of the times I just see a call stack full of anonymous functions which is a nightmare for me.
What is the best way to debug closure in javascript?
When I try to debug the javascript code which has a lot of closures I use to put a breakpoints.
Then I go to see the stack but most of the times I just see a call stack full of anonymous functions which is a nightmare for me.
What is the best way to debug closure in javascript?
Share Improve this question edited Feb 21, 2012 at 15:07 c69 21.6k8 gold badges55 silver badges83 bronze badges asked Feb 21, 2012 at 14:51 antonjsantonjs 14.3k15 gold badges70 silver badges91 bronze badges 1- it depends pletely on what the problem is...can you be more specific? – hvgotcodes Commented Feb 21, 2012 at 14:54
3 Answers
Reset to default 5You can add a name to the callback function. That way the function name will be shown during debugging.
As an example in jQuery
$('div').each( function divLoop() {
..
});
In OOP Javascript, it's mon to call the function as the name of the method
MyClass.prototype.methodName = function methodName() { ... }
Well, in Google Chrome, you can see variables content throughout closures:
Local is the current execution context
Closure is its enclosing execution context
...
Up to the global execution context
Instead of providing anonymous functions as your callback, try declaring a function and use that instead.
http://jsfiddle/v9Fas/
This way you can debug inside of the callback function just like a normal function call.