2013-09-30 61 views
1

說我有數組[func1,func2,func3]。函數列表的列表函數名稱

我想打印出一個字符串:「func1,func2,func3」。但是,它會打印該函數的全部內容。

我需要做一些正則表達式來從輸出中獲取名稱還是有更簡單的方法?

乾杯。

回答

2

使用Function name property

function doSomething() { } 

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

要知道,根據該文件,這並不在Internet Explorer中運行。如果這對你很重要,你可以看看other options

0

你想獲得列表中的函數名稱,對不對? 如果是這樣,這樣的事情應該適合你。請讓我知道,如果這不是你想要做的。 JsFiddle Working code here

//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(","));