Skip to content

Instantly share code, notes, and snippets.

@benvinegar
Created August 8, 2010 23:16
Show Gist options
  • Save benvinegar/514679 to your computer and use it in GitHub Desktop.
Save benvinegar/514679 to your computer and use it in GitHub Desktop.
Set JavaScript debugger breakpoints from the console
/**
* Utility lib for setting/unsetting JavaScript breakpoints
*
* Usage:
* breakpoint.set('globalMethodName');
* breakpoint.unset('globalMethodName');
*
* breakpoint.set('namespacedMethodName', namespaceObject);
* breakpoint.unset('namespacedMethodName', namespaceObject);
*/
var breakpoint = {};
(function() {
/**
* Set a breakpoint on the given method
*
* @param methodName {String} name of method to set breakpoint on
* @param namespace {Object} optional namespace obj; defaults to window
*/
breakpoint.set = function(methodName, namespace) {
namespace = namespace || window;
var orig = namespace[methodName];
var wrapper = function() {
debugger;
return orig.apply(this, arguments);
}
// Preserve original function reference on the
// wrapper function itself
wrapper._orig = orig;
namespace[methodName] = wrapper;
};
/**
* Remove a breakpoint from the given method (only works on
* breakpoints set by this library)
*
* @param methodName {String} name of method to remove breakpoint from
* @param namespace {Object} optional namespace obj; defaults to window
*/
breakpoint.unset = function(methodName, namespace) {
namespace = namespace || window;
// Restore original reference (if it exists)
if (typeof namespace[methodName]._orig === 'function') {
namespace[methodName] = namespace[methodName]._orig;
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment