Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS Bin // source https://jsbin.com/ruqezep
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<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);
};
};
// --- EXAMPLES ---
// wrapping the native JSON.parse function
wrapper(JSON, 'parse');
JSON.parse(JSON.stringify({ foo: 'bar', baz: 2 }));
// prints =>
// 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');
// prints =>
// MY CUSTOM PREFIX: hello from logMsg
</script>
<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);
};
};
// --- EXAMPLES ---
// wrapping the native JSON.parse function
wrapper(JSON, 'parse');
JSON.parse(JSON.stringify({ foo: 'bar', baz: 2 }));
// prints =>
// 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');
// prints =>
// MY CUSTOM PREFIX: hello from logMsg</script></body>
</html>
// 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);
};
};
// --- EXAMPLES ---
// wrapping the native JSON.parse function
wrapper(JSON, 'parse');
JSON.parse(JSON.stringify({ foo: 'bar', baz: 2 }));
// prints =>
// 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');
// prints =>
// MY CUSTOM PREFIX: hello from logMsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.