0

The below code works properly if is use function name directly and not as a variable with the function name.

I would make work this loop with the variable having the function name.

Please help !

function func_1() {
    alert("FUNCTION EXISTS");
}

function func_2() {
    alert("FUNCTION EXISTS");
}

var functions = ["func_1", "func_2", "func_3", "func_4"];

for (var i = 0; i < functions.length; i++) {
    var func_name = functions[i]; // THIS DOESNT WORK < THIS IS THE ONE I WOULD WANT TO WORK
    var func_name = func_3; // THIS WORKS

    if (typeof func_name === 'function') {
        alert("HELLO WORLD");
    }

    alert("ITERATING WELL");
}
beaver
  • 17,333
  • 2
  • 40
  • 66
Manu
  • 81
  • 9

1 Answers1

0

According to this answer by matt, you should "check whether it's defined in the global scope":

if (typeof window[func_name] === "function") {
    alert( '"HELLO WORLD", says ' + func_name );
}
Community
  • 1
  • 1