[ACCEPTED]-Programmatically control breakpoints in Javascript?-breakpoints

Accepted answer
Score: 30

First you could add a call to a function 16 like __checkDebug(); which will check for 15 a global (or semi-global) variable and when 14 said variable is true, call debugger.

  
function __checkDebug() {
   if (debugme) debugger;
}

all 13 of your functions you're concerned about 12 debugging would be like so:

  
function foo() {
   __checkDebug();

   //.... whatever foo was gonna do.
}

You can then 11 take it a little further and dynamically 10 decorate functions while the code is being 9 executed like so:


Function.prototype.debug = function(){   
   var fn = this; 
   return function(){     
       if (debugme) debugger; 
       return fn.apply(this, arguments);     
   }; 
}; 

foo = foo.debug();  

now any time foo is called 8 it will call debugger if the debugme variable 7 is truthy.

Another option would be to build 6 a javascript build system that injects the 5 call after every function declaration - this 4 requires a syntax parser but if you're only 3 looking to modify functions a simple tokenizer 2 for that use case is pretty easy to write 1 - but I'll leave that up to you.

Score: 28

You can use debugger; in code to make breakpoint 2 for firebug. For example:

alert('1');
debugger;
alert('2');

And firebug automatically 1 stops on this keyword.

Score: 1

Have a look at the FireBug functions debug(fn) & undebug(fn) names 2 which set a breakpoint on the first line 1 of the named function.

See point #6:

http://michaelsync.net/2007/09/30/firebug-tutorial-script-tab-javascript-debugging

More Related questions