最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Debugging closures in javascript - Stack Overflow

programmeradmin7浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

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 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.

发布评论

评论列表(0)

  1. 暂无评论