views:

1572

answers:

11

How do you tell if a function in Javascript is defined?

I want to do something like

function something_cool(text, callback){
    alert(text);
    if( callback != null ){ callback(); };
}

but that gets me a 'callback is not a function' error when callback is not defined.

+13  A: 
typeof(callback) == "function"
Tom Ritter
Not a problem as long as you like magic strings in your code.
Jason Bunting
This isn't really a magic string, since the set of typeof values is precisely defined in the ECMA spec. Although the syntax for this is a little goofy to begin with, it's perfectly reasonable idiomatic Javascript.
quixoto
@quixoto - understood, I guess what I mean is that you still need to have a string regardless - I would rather not have any more literal strings littering my code than absolutely needed; therefore, this isn't my ideal for testing if something is a function.
Jason Bunting
+1  A: 

try

if (typeof(callback) == 'function')
bdukes
+1  A: 

typeof(callback) == "function"

A: 

try:

if (!(typeof(callback)=='undefined')) {...}
Brian
+5  A: 

typeof is an operator, not a function, it does not require brackets/braces (although they also don't hurt anything).

if (typeof yourFunction === 'function') { ... }
Grant Wagner
+1  A: 
function something_cool(text, callback){
    alert(text);
    if(typeof(callback)=='function'){ 
        callback(); 
    };
}
ConroyP
+2  A: 

if (callback && typeof(callback) == "function)

Note that callback (by itself) evaluates to false if it is undefined, null, 0, or false. Comparing to null is overly specific.

Robin like the bird
+17  A: 

All of the current answers use a "magic" string - this does not:

function isFunction(possibleFunction) {
  return (typeof(possibleFunction) == typeof(Function));
}

Personally, I try to reduce the number of strings hanging around in my code...


Also, while I am aware that typeof is an operator and not a function, there is little harm in using syntax that makes it appear as the latter.

Jason Bunting
Everything about this answer is 100% correct!
Gary Willoughby
Works like a dream
bbeckford
Brilliant and probably generalizable to tons of situations in Javascript.
Yar
+1  A: 
if ('function' === typeof callback) ...
Andrew Hedges
A: 

self[callback] also works, but only on FF, this if what i'm using:

function isfn(x) { if (eval("typeof "+x+" == 'function'")) { return true; } else { return false; } }

Rodrigo
As a best practice and general rule, never use "eval" unless you have some insanely good reasons. Very bad idea, very bad. No room here to tell you why, but you should go read up on it.
Jason Bunting
A: 

Those methods to tell if a function is implemented also fail if variable is not defined so we are using something more powerful that supports receiving an string:

function isFunctionDefined(functionName) {
    if(eval("typeof(" + functionName + ") == typeof(Function)")) {
        return true;
    }
}

if (isFunctionDefined('myFunction')) {
    myFunction(foo);
}
patriciorocca