I want to create a function dynamically using javascript. I’ve started with the following:
function setFunc(setName){
var setName = function () {
};
}
setFunc("totop");
I want to set a function dynamically, but it doesn’t work!
How can I fix this?
That won’t work.
However, consider this: functions in JavaScript are just values and window is the top-level scope, so… (This assumes it is desired for the new function to be created in the top-level scope.)
However, I do not recommend this sort of global side-effect…
Happy coding.