-
-
Save adamnemecek/5cd92de04ee8769b265e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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