Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You have written a plugin and need to test whether one of the settings is a valid callback function.
(function($) {
$.fn.myPlugin = function(settings) {
return this.each(function() {
settings = $.extend({ onShow: null }, settings);
$(this).show();
if ( $.isFunction( settings.onShow ) ) {
settings.onShow.call(this);
}
});
};
$(document).ready(function() {
$('div').myPlugin({
onShow: function() {
alert('My callback!');
}
});
});
})(jQuery);While the JavaScript language provides the typeof operator, inconsistent results and edge cases across web browsers need to be taken into account. jQuery provides the .isFunction() method to ease the developer’s job. Worth pointing out is that since version 1.3, this method works for user-defined functions and returns inconsistent results with built-in language functions such as this:
jQuery.isFunction( document.getElementById );
which returns false in versions of Internet Explorer.