console.assert失败时中断chrome调试器

Break into chrome debugger when console.assert fails

本文关键字:chrome 调试器 中断 assert 失败 console      更新时间:2023-09-26

console.assert()失败时,我如何导致中断(除了用我自己的断言函数替换断言函数并在那里设置断点之外)?

当您第一次启动Chrome开发工具时

点击鼠标悬停的暂停图标"Do not pause on exceptions. Click to pause on all exceptions"位于Chrome开发工具最底部的栏中,靠近左侧。如果找不到该图标,请按Chrome开发工具中的Sources选项卡。

该图标应该变成蓝色。如果第一次它没有变成蓝色,请再次单击它。然后,它甚至会在console.assert(false); 上暂停

我不知道有什么方法可以在出现故障的assert() s上自动中断,至少在Chrome/Safari中是这样。

但我认为用自己的替换实际上也差不多:

var nativeAssert = console.assert;
function assertWrapper(expr) {
    nativeAssert.apply(this, arguments);
    if (!expr) {
        "";
    }
}
console.assert = assertWrapper;

然后,您可以在""行上放置一个断点来捕获所有故障,同时仍然可以获得您期望的标准控制台输出。