0

Inside a function I need to test if another variable function exists and then call it if it does.

function div1_load(){
  // do something
}

function main(id){
  var func = id + '_load';
  if (func is function) {
    func.call();
  }
}
main('div1');

I can think of two ways to test, but both involve eval() which I would like to avoid.

  1. typeof eval(func) === 'function'
  2. eval('typeof(' + func + ')') === 'function'

Is there a way without using eval() ? If not, which of my two methods is most secure ?

Kim
  • 2,747
  • 7
  • 41
  • 50
  • Why does it need to be a variable? Can it have different values? If so, why is the function a global instead of a member of a data structure (e.g. `{ div1_load: function () { ... }, div2_load....}` – Quentin Jun 20 '12 at 12:01
  • possible duplicate of [How to execute a JavaScript function when I have its name as a string](http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string) –  Jun 20 '12 at 12:05
  • Typical to find the answer after posting. @Quentin: div1_load() is not always defined, and there is no data structure because its being kept simple. – Kim Jun 20 '12 at 12:16

3 Answers3

2
function main(id, ns) {
    if (!ns) {
        ns = window;
    }
    if (typeof ns[id + '_load'] === "function") {
        ns[id + '_load']();
    }
}

function div1_load() {
    alert("div1_load() was called");
}

main('div1');

var test = {
    div1_load: function() {
        alert("test.div1_load() was called");
    }
};

main('div1', test);​
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • I found it here after I posted. http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string – Kim Jun 20 '12 at 12:04
  • What if the function is not global nor belongs to an object? Like, `(function() { function test() {} function main() { /* does test() exist? */ }());` – Florian Margaine Jun 20 '12 at 12:06
  • @FlorianMargaine: see edit. As for your example, the outer function is an anonymous function; probing its inner functions (IMO) makes no sense. – Salman A Jun 20 '12 at 12:08
2

Assuming the functions are global (i.e. belonging to window object, you can just check the typeof:

if(typeof window[id] === 'function') {  }
BenM
  • 52,573
  • 26
  • 113
  • 168
  • What if the function is not global nor belongs to an object? Like, `(function() { function test() {} function main() { /* does test() exist? */ }());` – Florian Margaine Jun 20 '12 at 12:07
  • I think you may be SOL, I'm afraid. The namespace to which the function is assigned is anonymous; isn't that the whole point of using closures? – BenM Jun 20 '12 at 12:10
1

if the function is assigned within the global namespace (window), you can check for a function name string being a function by using window[function name] instanceof Function. If it's assigned within a (pseudo) namespace, use SomeNamespace[function name]. In other words, a function can be retrieved as a property/method from a namespace (global (window in a browser), or defined in your code).

function div1_load(){
  // do something
}

function main(id){
  if (window[id+'_load'] instanceof Function) {
    window[id+'_load'].call();
  }
}
main('div1')

[edit based on comment] In case of an anonymous, immediately instantiated function, this won't work. The example from @Florian Margaine could be adjusted this way though:

var MyPersonalNamespace = (function() {
  function test() {}
  function main(someid) { 
     if (this[someid] instanceof Function){
       this[someid].call();
     }
  }
  return {test: test, main: main};
 }());
 MyPersonalNamespace.main('test');
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • What if the function is not global nor belongs to an object? Like, `(function() { function test() {} function main() { /* does test() exist? */ }());` – Florian Margaine Jun 20 '12 at 12:08