Is It Possible For A Javascript Function To Return Its Own Function Call As A String?
Solution 1:
I put this one up on jsFiddle: http://jsfiddle.net/pGXgh/.
functiongetOwnFunctionCall() {
var result = "getOwnFunctionCall(";
for (var i=0; i < arguments.length; i++) {
var isString = (toString.call(arguments[i]) == '[object String]');
var quote = (isString) ? "\"" : "";
result += ((i > 0) ? ", " : "");
result += (quote + arguments[i] + quote);
}
return result + ")";
}
alert(getOwnFunctionCall(5, "3", /(a|b|c)/));
Note that this should work for your example, but still needs work for arbitrarily complex objects/JSON included as a parameter.
Solution 2:
functionDisplayMyName()
{
//Convert function arguments into a real array then let's convert those arguments to a string.var args = [].slice.call(arguments).join(',');
// Get Function namevar myName = arguments.callee.toString();
myName = myName.substr('function '.length);
myName = myName.substr(0, myName.indexOf('('));
return(myName + " ("+ args + ")");
}
var functionText = DisplayMyName(5, "3", /(a|b|c)/) //returns DisplayMyName(5, "3", /(a|b|c)/)alert(functionText);
Solution 3:
Using the implicit arguments
variable, you can extract both the function arguments and the function name:
functiongetOwnFunctionCall() {
var args = arguments; // Contains the arguments as an arrayvar callee = arguments.callee; // The caller function// Use this to construct your string
}
Edit
Several comments note that callee
is not something to be relied on. But if this is something you are going to do inside each of your methods, then just use the function name as you have defined it:
var functionName = "getOwnFunctionCall"; // But you can really just use it inline...
Solution 4:
if you NEED to do it, and need to do it in global strict, and you don't want to hard-code the names:
functionargs(arg){
var me;
try{ badCAll654(); }catch(y){ me=String(y.stack).split("args")[1].split("\n")[1].trim().split("@")[0].replace(/^at /,"").split(" ")[0].trim() }
return me +"("+[].slice.call(arg).join(", ")+")";
}
functiongetOwnFunctionCall() {
"use strict";
returnargs(arguments);
}
getOwnFunctionCall(1,true, /dd/);
this can be a good debugging tool, but i would not recommend using it on production sites/apps; it's going to impact performance quite a bit. This pattern only works in chrome and firefox, but works under a global "use strict".
IE9 is less strict, so you can do the following:
functionargs(arg){
var me=arg.callee+'';
return me.split("(")[0].split("function")[1].trim() +"("+[].slice.call(arg).join(", ")+")";
}
functiongetOwnFunctionCall() {
"use strict";
returnargs(arguments);
}
getOwnFunctionCall(1,true, /dd/);
if you poly-fill the trim()s, it should also work in IE8. if you don't use strict, you can do even more cool stuff like log the function that called the function that's being logged. you CAN even rip that function's source to find calls to the logged function if you want the names of the arguments and not just the values. Complex and worthless, but possible.
again, you should really use this only for debugging!
Solution 5:
Based on your comment
I've been trying to find ways to prevent specific functions in eval statements from being evaluated, and this is one potential solution for that problem.
What you are asking for might not be what you really need. Why not just override the functions you want to prevent before eval
ing and restore them aferwards:
var blacklist = [ 'alert', 'setTimeout' ];
var old = {};
// Save the blacklisted functions and overwrite
blacklist.forEach(function(name) {
old[name] = window[name];
window[name] = function() {
console.log(name + ' has been disabled for security reasons');
}
});
eval('alert("Hello world")');
// restore the original functions
blacklist.forEach(function(name) {
window[name] = old[name];
});
Post a Comment for "Is It Possible For A Javascript Function To Return Its Own Function Call As A String?"