[英]Is it possible for a JavaScript function to return its own function call as a string?
在JavaScript中,函数是否可以将自己的函数调用作为字符串返回?
function getOwnFunctionCall(){
//return the function call as a string, based on the parameters that are given to the function.
}
我希望这个函数简单地将它自己的函数调用作为字符串返回(如果它甚至可以这样做):
var theString = getOwnFunctionCall(5, "3", /(a|b|c)/);
//This function call should simply return the string "getOwnFunctionCall(5, \"3\", "\/(a|b|c)\/")".
我把这个放在jsFiddle上: http : //jsfiddle.net/pGXgh/ 。
function getOwnFunctionCall() {
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)/));
请注意,这应该适用于您的示例,但仍需要将任意复杂对象/ JSON作为参数包含在内。
function DisplayMyName()
{
//Convert function arguments into a real array then let's convert those arguments to a string.
var args = [].slice.call(arguments).join(',');
// Get Function name
var 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);
使用隐式arguments
变量,您可以提取函数参数和函数名称:
function getOwnFunctionCall() {
var args = arguments; // Contains the arguments as an array
var callee = arguments.callee; // The caller function
// Use this to construct your string
}
编辑
一些评论指出, callee
不是可以依赖的东西。 但是,如果您要在每个方法中执行此操作,那么只需使用您定义的函数名称:
var functionName = "getOwnFunctionCall"; // But you can really just use it inline...
如果您需要这样做,并且需要在全局严格中执行,并且您不想对名称进行硬编码:
function args(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(", ")+")";
}
function getOwnFunctionCall() {
"use strict";
return args(arguments);
}
getOwnFunctionCall(1,true, /dd/);
这可以是一个很好的调试工具,但我不建议在生产网站/应用程序上使用它; 它会对性能产生很大的影响。 此模式仅适用于chrome和firefox,但在全局“use strict”下工作。
IE9不那么严格,所以你可以做到以下几点:
function args(arg){
var me=arg.callee+'';
return me.split("(")[0].split("function")[1].trim() +"("+[].slice.call(arg).join(", ")+")";
}
function getOwnFunctionCall() {
"use strict";
return args(arguments);
}
getOwnFunctionCall(1,true, /dd/);
如果你对trim()进行多重填充,它也应该在IE8中工作。 如果你不使用strict,你可以做更酷的事情,比如记录调用正在记录的函数的函数。 如果你想要参数的名称而不仅仅是值,你甚至可以翻录该函数的源来查找对已记录函数的调用。 复杂而毫无价值,但可能。
再次,你应该真的只用于调试!
根据您的评论
我一直试图找到防止评估eval语句中特定函数的方法,这是解决该问题的一种可能的解决方案。
你要求的可能不是你真正需要的。 为什么不在evaling之前覆盖你想要阻止的eval
并将其恢复为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];
});
函数是否可以将自己的函数调用作为字符串返回?
不能。你不能通过你的参数的表达式提取函数 - 你只能访问它们的值。 当然,您可以使用原始值模拟调用字符串,但您永远不知道它们是作为变量,文字还是整个表达式传递给函数。
也许,Mozilla的toSource
方法可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.