1

假设我有数组 [func1, func2, func3]。

我想打印为字符串:“func1, func2, func3”。但是,它会打印函数的全部内容。

我需要做一些正则表达式来从输出中获取名称还是有更简单的方法?

干杯。

4

2 回答 2

2

使用函数name属性

function doSomething() { }

alert(doSomething.name); // alerts "doSomething"

请注意,根据文档,这在 Internet Explorer 中不起作用。如果这对您很重要,您可以查看其他选项。

于 2013-09-30T00:23:35.253 回答
0

您想在列表中获取函数名称,对吗?如果是这样的话,这样的事情应该对你有用。如果这不是你想做的,请告诉我。 JsFiddle 工作代码在这里

//declare the dummy functions
function funcOne(){
    return null;
}
function funcTwo(){
    return null;
}
function funcThree(){
    return null;
}
//add the functions to the array
var functionArray=[funcOne,funcTwo,funcThree];
//declare an output array so we can then join the names easily
var output=new Array();
//iterate the array using the for .. in loop and then just getting the function.name property
for(var funcName in functionArray){
    if(functionArray.hasOwnProperty(funcName))
        output.push(functionArray[funcName].name);
}
//join the output and show it
alert(output.join(","));
于 2013-09-30T00:32:53.483 回答