Posted By

amischol on 05/05/09


Tagged

javascript jquery function


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

amischol


Check functions jQuery extension


 / Published in: jQuery
 

Hi, all, this is the "checkFunction" extension for jQuery.

This extension takes two parameters:

  1. fPointer This parameter is the "possible" function to check

     1.1   This parameter can be a String that is a function pointer   
                 ex:"editUser"
    
     1.2   This parameter can be a function
                 ex: function(idUser){alert('Edit User '+idUser);}
    
     1.3   This parameter can be a String that is a function body
                 ex: "alert('Edit User');"
    
  2. lastFpPointer
       This parameter is the function by default if the fpPointer is not a valid function
    
       2.1   This parameter must be a function
                  ex: function(){}
    

This extension makes possibles to get any possible representation of a function and detect if it exist.

If it exist return it like a correct function.

If doesn't exist the default function will be returned.

The use of this extension:

Examples:

 -  jQuery.checkFunction("EditUser", function(){});

   returns EditUser



 -  jQuery.checkFunction("EditUser ();alert('Edit Launched');", function(){});

   returns  function(){EditUser ();alert('Edit Launched');}



 -  jQuery.checkFunction(EditUser, function(){});

   returns  EditUser

Thanks.

  1. jQuery.extend(jQuery, {
  2. checkFunction: function(fPointer, lastFpPointer){
  3. var newFunction = false;
  4. if(jQuery.isFunction(fPointer)){
  5. newFunction = fPointer;
  6. }else if (/\(.*\)/igm.test(fPointer)){
  7. newFunction = new Function(fPointer);
  8. }else{
  9. try{
  10. var mayBeFunction = (eval(fPointer));
  11. if(jQuery.isFunction(mayBeFunction)){
  12. newFunction = mayBeFunction;
  13. }else{
  14. newFunction = lastFpPointer;
  15. }
  16. }catch(e){
  17. newFunction =lastFpPointer;
  18. }
  19. }
  20. return newFunction;
  21. });

Report this snippet  

You need to login to post a comment.