javascript - loop not working if function name is in a variable when checking if function exists -
this question has answer here:
- check if function exists name in string? 2 answers
the below code works if use function name directly , not variable function name.
i make work loop variable having function name.
please !
function func_1() { alert("function exists"); } function func_2() { alert("function exists"); } var functions = ["func_1", "func_2", "func_3", "func_4"]; (var = 0; < functions.length; i++) { var func_name = functions[i]; // doesnt work < 1 want work var func_name = func_3; // works if (typeof func_name === 'function') { alert("hello world"); } alert("iterating well"); }
according this answer matt, should "check whether it's defined in global scope":
if (typeof window[func_name] === "function") { alert( '"hello world", says ' + func_name ); }
Comments
Post a Comment