jQuery test for whether an object has a method?

This should work:

if (!!$.prototype.functionName)

try

if ($.fn.method) {
    $('a').method(...);
}

or

if ($.method) {
    $.method(...);
}

Because jQuery methods are prototype into a jQuery object, you can test it from the prototype object.

if( $.isFunction( $.fn.someMethod ) ) {
    // it exists
}

This uses the jQuery.isFunction()[docs] method to see if $.fn.someMethod is indeed a function. (In jQuery jQuery.fn is a reference to the prototype object.)

Tags:

Jquery