Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript函数是否可以以字符串形式返回自己的函数调用?_Javascript_Function - Fatal编程技术网

JavaScript函数是否可以以字符串形式返回自己的函数调用?

JavaScript函数是否可以以字符串形式返回自己的函数调用?,javascript,function,Javascript,Function,在JavaScript中,函数是否可以以字符串形式返回自己的函数调用 function getOwnFunctionCall(){ //return the function call as a string, based on the parameters that are given to the function. } 我希望这个函数以字符串的形式返回它自己的函数调用(如果可能的话): 使用隐式arguments变量,可以提取函数参数和函数名称: function getOwnF

在JavaScript中,函数是否可以以字符串形式返回自己的函数调用

function getOwnFunctionCall(){
    //return the function call as a string, based on the parameters that are given to the function.
}
我希望这个函数以字符串的形式返回它自己的函数调用(如果可能的话):


使用隐式
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
}
编辑

一些评论指出,
被调用方
是不可依赖的。但是,如果这是您在每个方法中要做的事情,那么只需使用您定义的函数名即可:

var functionName = "getOwnFunctionCall"; // But you can really just use it inline...

我把这个放在JSFIDLE:

function getOwnFunctionCall(){
var result=“getOwnFunctionCall(”;
for(var i=0;i0)?“,:”);
结果+=(引号+参数[i]+引号);
}
返回结果+”;
}
警报(getOwnFunctionCall(5,“3”/(a | b | c)/);
请注意,这应该适用于您的示例,但对于作为参数包含的任意复杂对象/JSON仍然需要进行处理。

根据你的评论

我一直在努力寻找防止eval中出现特定功能的方法 语句,这是一个潜在的解决方案 解决这个问题

您所要求的可能不是您真正需要的。为什么不在
eval
ing之前覆盖您想要阻止的函数,然后重新恢复它们:

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];
});
函数是否可以以字符串形式返回自己的函数调用

function getOwnFunctionCall(){
    //return the function call as a string, based on the parameters that are given to the function.
}
不可以。您无法通过什么表达式将参数提取到函数中-您只能访问它们的值。当然,您可以使用基元值模拟调用字符串,但您永远不知道它们是作为变量、文本还是整个表达式传递给函数的


也许,Mozilla's可以帮助您做到这一点。

如果您需要这样做,并且需要在全局范围内严格执行,并且您不想硬编码名称:

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,但在全局“严格使用”环境下工作

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()s,它也应该在IE8中工作。 如果你不使用strict,你可以做更酷的事情,比如记录调用被记录函数的函数。如果你想要参数的名称,而不仅仅是值,你甚至可以翻录该函数的源代码来查找对被记录函数的调用。复杂而毫无价值,但这是可能的


同样,您应该只在调试时使用它!

对于复杂对象的实例参数,您会怎么做?不会。您有什么特别想做的吗?@sciritai我一直在试图找到方法来防止对
eval
语句中的特定函数求值,这是一个潜在的解决方案或者那个问题。@AndersonGreen清理JavaScript非常困难,如果不是理论上不可能的话,如果之前不执行它的话。每当我看到“是否[空白]可能”的问题,我总想给出一个简单的答案:“是的。”大多数技术问题都有可能的解决方案。如果你真的想让别人展示如何完成任务,也许如何“问题在将来会更好。在严格模式下,这将失败。被调用方不再是arguments对象的有效属性。应该注意的是,
参数。被调用方
在严格模式下是不允许的,规范编写者不赞成。当字符串、正则表达式和数字用作参数时,此函数可以正常工作。但是,当函数用作参数时,它会打印函数的源,而不是函数的名称。我会看看是否能解决这个问题。是的,格式化每种对象类型肯定需要特殊情况。如果传递了一个对象,是否需要原始对象的变量名?当函数用作参数时,这不起作用:返回警报的本机代码…无论如何,您不应该为参数的变量
alert
赋值。为什么要删除它们?就让他们空出私人空间吧。例如:函数(代码){var alert,prompt;eval(代码)}
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/);
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/);