Dynamic function name in javascript?

The Solution to Dynamic function name in javascript? is


This will basically do it at the most simple level:

"use strict";
var name = "foo";
var func = new Function(
     "return function " + name + "(){ alert('sweet!')}"
)();

//call it, to test it
func();

If you want to get more fancy, I have a written an article on "Dynamic function names in JavaScript".

~ Answered on 2012-03-30 17:39:30


Most Viewed Questions: