javascript 在处理未定义的变量时如何让 Chrome 调试器中断或出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19306702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:06:23  来源:igfitidea点击:

How to get Chrome debugger to break or error when working on undefined variables

javascriptgoogle-chrome-devtoolsjavascript-debugger

提问by Tony_Henrich

My Javascript code (hundreds of lines) hangs Chrome and when I debug the issue I find out that a variable was undefined. I don't get errors in the console. So this is making my debugging more time consuming because there are no errors or exceptions or anything that tells me where the issue is.

我的 Javascript 代码(数百行)挂起 Chrome,当我调试问题时,我发现一个变量未定义。我没有在控制台中收到错误消息。所以这让我的调试更加耗时,因为没有错误或异常或任何告诉我问题出在哪里的东西。

I don't want to add debugging code. Is there a way to make the debugger put out an error, break in the debugger or give an exception or show anything useful for the developer when hitting an undefined variable during runtime? It doesn't have to be for Chrome only.

我不想添加调试代码。在运行时遇到未定义的变量时,有没有办法让调试器发出错误、中断调试器或给出异常或显示对开发人员有用的任何东西?它不一定只适用于 Chrome。

回答by Ethan Selzer

You can break into the DevTools debugger when a JavaScript error occurs using the Pause on JavaScript Exceptionsfeature. It has two active modes; pause on all exceptions, and pause on uncaught exceptions.

当发生 JavaScript 错误时,您可以使用Pause on JavaScript Exceptions功能闯入 DevTools 调试器。它有两种活动模式;暂停所有异常,暂停未捕获的异常。

Based on the description of your experience, the application you are working on may have errors that are caught but not re-thrown or logged. Using the "Pause on All Exceptions" (blue colored pause icon), will help in this scenario.

根据您的经验描述,您正在处理的应用程序可能存在已捕获但未重新抛出或记录的错误。在这种情况下,使用“暂停所有异常”(蓝色暂停图标)将有所帮助。

Note: some libraries, like jQuery, catch exceptions and do not re-throw them. If you have this experience, you may need to advance past these exceptions or set the "Pause on All Exceptions" feature after all dependencies have loaded.

注意:一些库,比如 jQuery,会捕获异常并且不会重新抛出它们。如果您有这样的经验,您可能需要在所有依赖项加载后跳过这些异常或设置“暂停所有异常”功能。

回答by Tony_Henrich

window.onerror = function() { debugger; }