Created
February 7, 2019 12:01
-
-
Save alessioalex/2b6100c056f4b02f27c872ec7f9d5f17 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/ruqezep
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
<!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> |
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
// 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