Debug closing in javascript

When I try to debug javascript code that has many closures, I use to set breakpoints.

Then I go to the stack, but most of the time I see a call stack full of anonymous functions, for me it's a nightmare.

What is the best way to debug closing in javascript?

+4
source share
3 answers

Well, in Google Chrome you can see the contents of variables at close time:

enter image description here

Local - current execution context
Closing is the surrounding execution context
...
Before the context of global execution

+2
source

You can add a name to the callback function. Thus, the function name will be displayed during debugging.

As an example in jQuery

$('div').each( function divLoop() { .. }); 

In OOP, Javascript usually calls a function by the name of the method name.

 MyClass.prototype.methodName = function methodName() { ... } 
+5
source

Instead of providing anonymous functions as a callback, try declaring a function and using it instead.

http://jsfiddle.net/v9Fas/

This way you can debug the inside of the callback function like a regular function call.

+1
source

Source: https://habr.com/ru/post/1397625/


All Articles