Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging closures 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?

like image 375
antonjs Avatar asked Feb 21 '12 14:02

antonjs


1 Answers

You 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 common to call the function as the name of the method

MyClass.prototype.methodName = function methodName() { ... }
like image 180
Davide Avatar answered Sep 21 '22 06:09

Davide