0

I am dynamically loading functions as needed in my web app. When I request these functions as I need them, I'd like to check if they already exist. So I pass in an array of the function names such as ['Result','Question']. I then loop through that array and would like to see if it's typeof is a function, if so, then I know I don't need to load it.

Here is a simplified version:

function Result(){}
var functionName = 'Result';
if (typeof functionName == 'function'){
    alert('function exists, don't load it');
else
    alert('function does not exist, load it');

I know the above example doesn't work because I need the value of functionName, not functionName itself. Any ideas how to do this?

Scott Chantry
  • 1,115
  • 5
  • 16
  • 29
  • Why not add them as objects rather than arrays? Then you can do object.keys and see if the value resides there. Also doing a `typeof` on a string (functionName) will yield string every time. – AlbertEngelB Jul 03 '13 at 18:48
  • @Scott you could `eval` the `functionName` variable (perhaps with a validation regex check beforehand, for safety), and then take the `typeof` the result of that. – feralin Jul 03 '13 at 18:48

4 Answers4

3

Supposing that your function is global you can verify if it exists using window object (the object where are stored the global variables).

 if (typeof window[functionName] === 'function') {
     // function exists
 }

JSFIDDLE

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
3

You can use window[functionName] as follows:

function Result(){}
var functionName = 'Result';
if (typeof window[functionName] == 'function'){
    alert("function exists, don't load it");
          }else{
    alert("function does not exist, load it");
}

However you need to make sure you escape the single quote in don't or use double quotes since it terminates your String literal.

Here is a working example: http://jsfiddle.net/WN92K/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

You may do this and see if your function is global.

if (window[functionName] instanceof Function){
mohkhan
  • 11,925
  • 2
  • 24
  • 27
0

You can use typeof and try catch : (window[fctName] doesn't always works because you aren't always in the window "scope" or you need to test object members (or simulated namespace etc.))

function isFunction(fctName) {
    try {
        return typeof(eval(fctName)) === "function";
    } catch (err) {
        return false;
    }
}

function isFunction2(fctName) {
    return typeof(window[fctName]) === "function";
}

var myFunc1 = function () {};
function myFunc2() {};

var aString = "hello world!";

console.log(myFunc1); // function () {}
console.log(myFunc2); // function myFunc2() {}
// Can't work : Exception throw
// console.log(myFunc3);

console.log(window['myFunc1']); // function () {}
console.log(window['myFunc2']); // function myFunc2() {}
console.log(window['myFunc3']); // undefined

console.log(isFunction("myFunc1"));  // true
console.log(isFunction("myFunc2"));  // true
console.log(isFunction("myFunc3"));  // false

console.log(isFunction2("myFunc1"));  // true
console.log(isFunction2("myFunc2"));  // true
console.log(isFunction2("myFunc3"));  // false

console.log(isFunction('aString.toLowerCase'));  // true
console.log(isFunction('aString.toLower'));      // false : toLower doesn't exist

console.log(isFunction2('aString.toLowerCase')); // false : aString.toLowerCase isn't defined ! (but aString was)
console.log(isFunction2('aString.toLower'));     // false : same reason than above

// So Eval can be usefull

http://jsfiddle.net/L6Q9N/2

Sirus64
  • 46
  • 4