Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 991
cheziHoyzer Avatar asked Dec 01 '25 03:12

cheziHoyzer


2 Answers

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).

like image 64
Bergi Avatar answered Dec 02 '25 15:12

Bergi


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
like image 44
Mritunjay Avatar answered Dec 02 '25 16:12

Mritunjay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!