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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.