If you want to dynamically generate your function without eval , you can use the constructor
Function([arg1[, arg2[, ... argN]],] functionBody)
That way you can do things like
var func = new Function('message', 'alert("Hello, " + message);') func('world!');
See MDC for details.
Greetings
Note I had never used this approach before, and I had never used the Function () constructor before. Therefore, I do not know if there may be other shortcomings.
source share