我想通过代码强制Chrome调试器在一行上中断,或者使用某种注释标记,例如console.break()。
当前回答
调试器是EcmaScript保留的关键字,从ES5开始提供可选语义
因此,它不仅可以在Chrome中使用,还可以通过节点调试myscript.js在Firefox和node .js中使用。
标准说:
Syntax DebuggerStatement : debugger ; Semantics Evaluating the DebuggerStatement production may allow an implementation to cause a breakpoint when run under a debugger. If a debugger is not present or active this statement has no observable effect. The production DebuggerStatement : debugger ; is evaluated as follows: If an implementation defined debugging facility is available and enabled, then Perform an implementation defined debugging action. Let result be an implementation defined Completion value. Else Let result be (normal, empty, empty). Return result.
ES6没有变化。
其他回答
调试器是EcmaScript保留的关键字,从ES5开始提供可选语义
因此,它不仅可以在Chrome中使用,还可以通过节点调试myscript.js在Firefox和node .js中使用。
标准说:
Syntax DebuggerStatement : debugger ; Semantics Evaluating the DebuggerStatement production may allow an implementation to cause a breakpoint when run under a debugger. If a debugger is not present or active this statement has no observable effect. The production DebuggerStatement : debugger ; is evaluated as follows: If an implementation defined debugging facility is available and enabled, then Perform an implementation defined debugging action. Let result be an implementation defined Completion value. Else Let result be (normal, empty, empty). Return result.
ES6没有变化。
正如其他人已经说过的,调试器;才是正确的选择。 我写了一个小脚本,你可以在浏览器的命令行中使用它来设置和删除函数调用之前的断点: http://andrijac.github.io/blog/2014/01/31/javascript-breakpoint/
调试JavaScript代码的方法有很多。以下两种方法被广泛用于通过代码调试JavaScript
使用console.log()在浏览器中打印出值 控制台。(这将帮助您理解某些点上的值 你的代码) 调试器的关键字。添加调试器;去你想去的地方 调试,然后打开浏览器的开发人员控制台并导航到 来源选项卡。
有关调试JavaScript代码的更多工具和方法,请参见W3School提供的这个链接。
这是可能的,你想这么做的原因有很多。例如,在页面加载开始时调试javascript无限循环,会阻止chrome开发人员工具集(或firebug)正确加载。
参见第2节
http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/
或者只是在所需的测试点向代码中添加一行包含调试器字样的代码。
设置按钮点击监听器并调用调试器;
例子
$("#myBtn").click(function() {
debugger;
});
Demo
http://jsfiddle.net/hBCH5/
JavaScript中调试的资源
http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/ http://berzniz.com/post/78260747646/5-javascript-debugging-tips-youll-start-using-today
推荐文章
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 卸载Chrome开发工具
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- CSS变换,锯齿边缘在铬
- 如何禁用文本选择使用jQuery?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?