在 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/ 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。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)