I’m curious, what are the (performance) differences in the ways that you can write a jQuery plugin, if any?
I’ve seen it done a couple of ways:
1. Using $.extend():
(function($){
$.fn.extend({
newPlugin: function(){
return this.each(function(){
});
}
});
})(jQuery);
2. Your own function:
(function($){
$.fn.newPlugin = function(){
return this.each(function(){
});
}
})(jQuery);
IMHO the second way is a bit cleaner and easier to work with, but seems like there could be some advantage to writing it with $.extend()? Or am I over-thinking this, there is no discernible difference and it’s just a matter of personal preference?
(I would have thought this would have been asked before, but I can’t find it — if it’s out there, please direct me to it)
well, consider that when you do
$.extendyou’re calling this method:but when you’re calling
$.fn.newPlugin, you’re simply appending (or possibly overwriting) data on jQuery’s prototype object, which is pretty much the exact same thing that$.fn.extendis doing anyways…seems pretty self-explanatory which method has less overhead (the latter).