Execute Functions...solo or Array

I have found myself multiple times having to "re-invent" this methodology to execute a single pass-through function or an array, pending on the situation.   For myself, and to share with others, I am posting this code-snippet for consumption.


 /****  
      @func - function() or [function(), function(),...]  
 ****/  
 function ExecuteFunction( func ) {  
      if ( typeof func !== 'undefined' && func != null ) {  
           if ( typeof func === 'function' ) {  
                func();  
           } else {  
                if ( func instanceof Array ) {  
                     func.forEach( function ( fn ) {  
                          ExecuteFunction( fn );  
                     } );  
                }  
           }  
      }  
 }  

And yes you can have a nested array of functions.  If some crazy reason you wanted to do this:


 var func = [  
      function(){  
      //do something  
      }, [  
           function () {  
                //do something else  
           },  
           function () {  
                //do something elsewhere  
           }  
      ],  
      function () {  
           //do something lastly  
      }  
 ]  

Popular posts from this blog

SysInternals - BgInfo for ALL Users

JSON/AJAX Helpers

Acquiring List of controls - Classic JS