最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to set a breakpoint in standardnative JavaScript functions? - Stack Overflow

programmeradmin1浏览0评论

Can I set a breakpoint on a standard JavaScript function? For example, can I pause the debugger every time context.beginPath() is called? Or every time that String.replace() is called?

UPDATE: What I meant by standard JavaScript function is functions built-in into the JavaScript engines.

Can I set a breakpoint on a standard JavaScript function? For example, can I pause the debugger every time context.beginPath() is called? Or every time that String.replace() is called?

UPDATE: What I meant by standard JavaScript function is functions built-in into the JavaScript engines.

Share Improve this question edited Oct 23, 2017 at 5:44 kiewic asked Oct 23, 2017 at 5:08 kiewickiewic 16.4k14 gold badges84 silver badges104 bronze badges 1
  • This maynot be useful but you can take a look at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – brk Commented Oct 23, 2017 at 5:25
Add a comment  | 

5 Answers 5

Reset to default 13

Yes you can do this by overriding the original functionality by performing the following two steps:

Make a copy(reference really) of the original function:

mylog = console.log;

Override the original with your copy inserting the debugger statement:

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

Now when called console.log will perform a breakpoint. (Note you'll have to handle different function arguments differently depending on the function be overriden)

Here is another example using an instance methods, for example 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'));

In Chrome the convenience function window.debug() (only available from the console unless you assign it to a variable) allows setting breakpoints on builtin (or user-defined) functions:

debug(console.log)
console.log("Hello")

Shows the Sources pane and says

发布评论

评论列表(0)

  1. 暂无评论