(function ($) {
$.fn.test = function () {
console.log('works');
};
})(jQuery);
$(document).ready(function () {
var var1 = $().test(); // works
var var1 = $.test(); // error: $.test is not a function
});
Is it possible to call like this $.test()?
You could easily extend the
jQueryobject itselfBut be aware that this function will not be available in
jQuery.fn(which equalsjQuery.prototype) and therefore, you can’t accessdom nodesin there viathis, neither could you chain that method after or before other jQuery plugin functions.The reason why
$().test()works is, that the jQuery constructor function creates a new object which is linked tojQuery.fn(prototype..).