Last active
December 29, 2015 02:39
-
-
Save sadasant/7601827 to your computer and use it in GitHub Desktop.
JavaScript Tools
This file contains hidden or 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
// Change a function to log arguments and return | |
function log_args_return(f){ | |
var s=f.toString(), | |
n=f.name, | |
_n="_"+n; | |
return "function "+n+"(){"+ | |
s.replace(n,_n)+ | |
";console.log('"+n+" args', Array.prototype.slice.call(arguments));"+ | |
"var r="+_n+".apply(null, arguments);"+ | |
"console.log('"+n+" return', r);return r}" | |
} | |
// The easiest way to use it and recover the value in "_"+function_name | |
var function_name = "log_args_return"; | |
eval("_$=$;eval(log_args_return($))".replace(/\$/g,function_name)) | |
// Format variables in a string | |
// "Hello $, my name is $".replaceEach("$", "SOMEONE", "ME") | |
String.prototype.replaceEach=function (m){var r=this,i=0;while(r.indexOf(m)>=0){r=r.replace(m, arguments[i++])};return r} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment