cheziHoyzer
cheziHoyzer

Reputation: 5021

JavaScript - Best practice to check is function

I saw many kinds of methods for checking callback function before calling.

1)

 function(callBack)    
      {    
           ...
           if(callBack != null)
              callBack();   
      }      

2)

  function(callBack)    
          {    
               ...
               if(typeof callBack == 'function')
                  callBack();   
          }  

3)

  function(callBack)    
          {    
               ...
               if(callBack !== null)
                  callBack();   
          } 

4)

  function(callBack)    
          {    
               ...
               if(callBack != undefined)
                  callBack();   
          }

5)

   function(callBack)    
          {    
               ...
               try{    
                  callBack();  
                  } catch(err) {}

          }

6)

   function(callBack)    
          {    
               ...
                if (callBack && typeof(callBack) == "function")
                  callBack();   

          }

7)

    function(callBack)    
          {    
               ...
                if(callBack)
                  callBack();   

          }

8)

    function(callBack)    
              {    
                   ...
                    if(typeof window[callBack] === 'undefined')
                      callBack();   

              }

9)

  function(callBack)    
              {    
                   ...
                    if(callBack instanceof Function)
                      callBack();   

              }

I believe there is more...

What is the best way for checking if the given 'callBack' parameter is a function?

And why not to use the other examples.

Upvotes: 3

Views: 8888

Answers (4)

Gyanendra Agnihotri
Gyanendra Agnihotri

Reputation: 1

if($.isFunction(callback)) {
    callback();
}

Use $.isFunction(callback) to check callback is of function type or not. this also checks for null/empty/undefined values. if callback is of type function then returns true else false

Upvotes: 0

Bergi
Bergi

Reputation: 664297

if (callBack != null)

Sure, why not. Notice there are still many non-function values that pass this.

if (typeof callBack == 'function')

That's exactly what you want.

if (callBack !== null)

No. Does not work when callBack is undefined.

if (callBack != undefined)

Rather not. See How to check for "undefined" in JavaScript? for why null is favoured (though equivalent).

try{ callBack(); } catch(err) {}

Uh, possibly slower and swallowing any errors in the callback. If that is what you want, you should still combine it with one of the other tests.

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

Redundant, but possibly faster than a mere typeof test. You will want to test this if you care about performance, otherwise there's hardly a reason to be so verbose.

if (callBack)

Sure, why not. Notice there are still many truthy non-function values that pass this.

if (typeof window[callBack] === 'undefined')

No. This is absolutely doing a very different thing.

if (callBack instanceof Function)

This should work for everything but some extreme edge cases, but using typeof is safer (and faster).

Upvotes: 11

Maryo
Maryo

Reputation: 503

Try with you Firefox or Chrome console

typeof someFunc === "function"

instanceof is slower

you can check performance here : jsperf.com

Upvotes: 0

Mritunjay
Mritunjay

Reputation: 25882

I think strict check fortypeof function

typeof(callback)==='function'.

Just wanted to point out these things also give type of function

typeof(Object)==='function' //returns true
typeof(Array)==='function' //returns true
typeof(String)==='function' //returns true
typeof(Date)==='function' //returns true
typeof(Object)==='function' //returns true

Upvotes: 2

Related Questions