admin管理员组

文章数量:1310461

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)

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)

Share Improve this question edited Aug 5, 2010 at 9:18 Reigel Gallarde 65.3k21 gold badges125 silver badges142 bronze badges asked Aug 5, 2010 at 8:37 57ar7up57ar7up 4428 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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

Yes,

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

if ($(selector).exists()) {
    // code........
}

本文标签: javascriptCheck if jQuery method existsStack Overflow