如何在 standard/native JavaScript 函数中设置断点?

How to set a breakpoint in standard/native JavaScript functions?

我可以在标准 JavaScript 函数上设置断点吗?例如,我可以在每次调用 context.beginPath() is called? Or every time that String.replace() 时暂停调试器吗?

更新:我所说的标准JavaScript函数是JavaScript引擎中内置的函数。

您在寻找调试器语句吗?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger

是的,您可以通过执行以下两个步骤覆盖原始功能来做到这一点:

复制(真正参考)原函数:

mylog = console.log;

用插入 debugger 语句的副本覆盖原件:

console.log = function(){
    debugger;
    mylog.apply(this, arguments);
}

现在调用时 console.log 将执行断点。 (请注意,您必须根据被覆盖的函数以不同方式处理不同的函数参数)

这是另一个使用实例方法的例子,例如String.prototype.replace:

let originalFunction = String.prototype.replace;
String.prototype.replace = function(...args) {
    debugger;
    return originalFunction.call(this, ...args);
}

console.log('foo bar baz'.replace('bar', 'BAR'));

设置断点和调试代码的方法有3种。

1. Chrome 开发工具 / Firebug:

Using Chrome developer tools or firebug to locate the line of JavaScript, then set the breakpoint with the mouse. In chrome, you should first open(ctrl+shirt+I) to open developer tools.

Select the script tab or click on (ctrl+P) to open then desired file.

Search the line on which you wanted to set a breakpoint and set a breakpoint.

Whenever you execute your code next time in a browser, the breakpoint is fired. In watch section, you may see every expression, all variables in scope, and the call stack too.

2。调试器

Using debugger statement, it fires every time and it helps when it hard to find the execution of code.

debugger;

3。 Webstorm IDE / Visual Studio 代码

Webstorm IDE/Visual Studio Code have the facility to debug a code from IDE.

Javascript 是一种非常灵活的语言,可能遵循覆盖现有 javascript 和调试方法的方法,然后请使用以下调试方法。

var fnSetAttribute = Element.prototype.setAttribute; 
Element.prototype.setAttribute = function(name, value) {
    if (name == 'clone') {
        debugger; /* break if script sets the 'clone' attribute */
    }
    fnSetAttribute.call(this,name,value); /* call original function to
    ensure those other attributes are set correctly */
};

更多参考请查看https://alistapart.com/article/advanced-debugging-with-javascript