问题描述
所以我经常使用JavaScript,我想知道在调试javascript中是否有任何好的提示或技巧。例如,我有一个很好的,我经常使用。
So I use JavaScript fairly regularly and I was wondering if there were any good tips or tricks in debugging javascript....Like for example I have a good one I use fairly regularly.
在我的代码中,我会做这样的事情
In my code i would do something like this
window.p = $(this);
在控制台中我可以像我这样进行过滤
and in the console I can do my filtering like this
window.p.next("li").find("a").val();
或者我需要做的任何事情,但这样可以轻松地调试代码,我知道有很多提示和技巧在那里....感谢提前
or whatever i need to do...but this makes it easy to debug a specific spot in the code and I know there has got to me many tips and tricks out there....thanks in advance
另外我看着这个,虽然它有帮助,我正在寻找更多的技术,像我上面的...
Also i looked at this question and though its helpful i was looking more for techniques like my above ...
推荐答案
我通常设置一个断点并查看范围变量,或添加自己的观察表达式或将鼠标悬停在变量上,然后逐步浏览代码。
I usually set a breakpoint and look at the Scope Variables, or add my own watch expressions or hover over the variables and just step through the code.
有时我发现使用。
Sometimes I find it useful to just log things using console.log()
.
其他时候,当事情工作,但是他们太慢我使用code> console.profile()和 console.profileEnd()
Other times, when things work, but they are too slow I use console.profile()
and console.profileEnd()
一个有用的技巧是使用 $ 0
。如果您在HTML pannel中选择了一个元素,则可以在控制台中将其作为$ 0引用。
A useful trick is the use of $0
. If you have an element selected in the HTML pannel, you can reference it in the console as $0.
以上所有技术均适用于Firefox + Firebug和Webkit浏览器如Chrome和Safari)。最近我更喜欢Chrome,因为它允许我直接编辑代码(请参阅教程 )
All techniques above work in Firefox+Firebug and Webkit based browsers (such as Chrome & Safari). Lately I prefer Chrome because it allows me to edit code inline (check this tutorial by Paul Irish)
对于jQuery的额外调试帮助,我使用 FireQuery
For extra debugging help for jQuery I use FireQuery
这篇关于调试javascript有什么好的技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!