<meta name="viewport" content="width=device-width">
<h3>Check your console to see the output, press F12 and go to the console tab.</h3>
You can also <a href="https://jsbin.com/ruqezep/edit">view and edit the code</a> if you want.
<script id="jsbin-javascript">
// ES5 so you don't have to care about the browser
var wrapper = function(context, fnName, modifierFunction) {
var originalFn = context[fnName];
// a nice default function in case you just want to display
// the arguments and not modify them yourself
var modifierFn = modifierFunction || function(fn, args) {
console.log(fnName + ' called with arguments: ' + args);
return fn.apply(this, args);
context[fnName] = function() {
var args = Array.prototype.slice.call(arguments);
return modifierFn.call(this, originalFn, args);
// wrapping the native JSON.parse function
JSON.parse(JSON.stringify({ foo: 'bar', baz: 2 }));
// parse called with arguments: ["{"foo":"bar","baz":2}"]
// wrapping a logger function to add a prefix
var logMsg = function(message, details) {
console.log(message, details);
wrapper(window, 'logMsg', function modifierFn(originalFn, args) {
// modify the log message to prepend
args[0] = 'MY CUSTOM PREFIX: ' + args[0];
return originalFn.apply(this, args);
logMsg('hello from logMsg');
// MY CUSTOM PREFIX: hello from logMsg
<script id="jsbin-source-javascript" type="text/javascript">// ES5 so you don't have to care about the browser
var wrapper = function(context, fnName, modifierFunction) {
var originalFn = context[fnName];
// a nice default function in case you just want to display
// the arguments and not modify them yourself
var modifierFn = modifierFunction || function(fn, args) {
console.log(fnName + ' called with arguments: ' + args);
return fn.apply(this, args);
context[fnName] = function() {
var args = Array.prototype.slice.call(arguments);
return modifierFn.call(this, originalFn, args);
// wrapping the native JSON.parse function
JSON.parse(JSON.stringify({ foo: 'bar', baz: 2 }));
// parse called with arguments: ["{"foo":"bar","baz":2}"]
// wrapping a logger function to add a prefix
var logMsg = function(message, details) {
console.log(message, details);
wrapper(window, 'logMsg', function modifierFn(originalFn, args) {
// modify the log message to prepend
args[0] = 'MY CUSTOM PREFIX: ' + args[0];
return originalFn.apply(this, args);
logMsg('hello from logMsg');
// MY CUSTOM PREFIX: hello from logMsg</script></body>