views:

54

answers:

3

Is it possible? No matter how, in Javascript or jQuery.

For example: $.isFunction($.isFunction());

Upd: But how to check method of a jQuery plugin? Sometimes it not ready at the moment of it call and generates error. Example: $.isFunction($().jqGrid.format)

A: 

Yes,

jQuery.fn.exists = function(){return jQuery(this).length>0;}

if ($(selector).exists()) {
    // code........
}
Mike Johnson
+3  A: 

To pass a function to another function, leave the () off:

$.isFunction($.isFunction);   // true!

When you write () you are calling the function, and using the result it returns. $.isFunction() with no argument returns false (because undefined isn't a function), so you are saying $.isFunction(false), which is, naturally, also false.

I wouldn't bother using isFunction merely to check for the existence of something, unless you suspect that someone might have assigned a non-function value to it for some reason. For pure existence-checking, use the in operator:

if ('isFunction' in $) { ...
bobince
A: 
var checkfunc = function(container, fnc){
    if(container && fnc){
       if(container[fnc] && typeof container[fnc] === 'function')
          return(true);
    }
    return(false);
}

checkfunc($, 'isFunction');  // === true
jAndy