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
5 Answers
Reset to default 13Yes 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