Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if jQuery method exists

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)

like image 695
57ar7up Avatar asked Aug 05 '10 08:08

57ar7up


People also ask

How do you check if is exists or not in jQuery?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.

How do you know if a function exists?

To prevent the error, you can first check if a function exists in your current JavaScript environment by using a combination of an if statement and the typeof operator on the function you want to call.

Do methods exist in JavaScript?

JavaScript methods are actions that can be performed on objects. A JavaScript method is a property containing a function definition. Methods are functions stored as object properties.

How do you check if an object is a function JavaScript?

Use the typeof operator to check if an object contains a function, e.g. typeof obj. sum === 'function' . The typeof operator returns a string that indicates the type of the value. For functions, the operator returns a string containing the word function.


1 Answers

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 $) { ...
like image 165
bobince Avatar answered Oct 12 '22 05:10

bobince