1

Say I have the array [func1, func2, func3].

I would like to print out as a string: "func1, func2, func3". However, it prints the entire contents of the function.

Would I have to do some regex to grab the name from the output or is there an easier method?

Cheers.

Peach Passion
  • 457
  • 3
  • 11

2 Answers2

2

Use the Function name property:

function doSomething() { }

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

Be aware, that according to the documentation, this does not work in Internet Explorer. You can look into other options if that is important for you.

Community
  • 1
  • 1
Nicole
  • 32,841
  • 11
  • 75
  • 101
0

You want to get the function names in a list, right? If that is the case, something like this should work for you.Please let me know if this is not what you wanted to do. 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(","));
rowasc
  • 320
  • 1
  • 3
  • 10