views:

189

answers:

3

I've seen alot of jQuery implementations of existent JavaScript functions that merely wrap the JavaScript code in a jQuery wrapper and don't actually rely on any of jQuery's base for their operation.

What are the benefits of using Javascript as a jQuery plugin?
If there are none is there a speed loss to use a jQuery plugin that could have easily been implemented outside the wrapper just as well?

Many thanks in advance (just trying to learn something here).

Updated with example:
http://plugins.jquery.com/project/base64
was originally
http://www.webtoolkit.info/javascript-base64.html

+8  A: 

Much of jQuery is just a clever wrapper around existing JavaScript functions. $('#some-element') is perhaps a little easier to type than document.getElementById('some-element') but is otherwise not much different.

(I exaggerate, but only slightly.)

The main utility of jQuery is being able to combine together its various components. If I can select elements with a jQuery selector and then perform some action on those elements directly through a jQuery function, that's preferable to having to extract the underlying DOM elements and manipulate them manually, for example.

So it really depends on what functions you're seeing get wrapped. Some of them might very well add no value, and the authors are simply accustomed to everything being in jQuery. (We definitely see that phenomenon on StackOverflow — people who can't find a standard JavaScript function simply because it's not in the jQuery documentation). In other cases, there might be some hidden benefit even if the wrapper does little if anything to modify the underlying function's behavior.

VoteyDisciple
One of the main benefits of using a library like jQuery is also the fact that it handles browser detection, and abstracts things like selecting/manipulating elements. This is a huge benefit for things like ajax, and CSS. For example, if you call $(selector).css('opacity', 0.5), it just works, you don't have to worry about setting up a filter rule for IE.
Harold1983-
@Harold1983, "handles browser detection", where? ... jQuery is all feature-detection/inference.
J-P
He probably meant internally as in, it's intelligent enough to work across all browsers.
Mohammad
+4  A: 

There's also a lot of momentum around jQuery, and general trust. Including arbitrary javascript in your code base may not be as 'acceptable' to higher-up-types as including a new jQuery plugin.

So it may be a mistaken perception, but by being a jQuery plugin, a library benefits by being associated with high quality code.

joeslice
I vote up your answer because it's true, but at the same time, oh so sad :(
SBUJOLD
A: 

IMHO the only reason to create a plugin is to execute some functionality against a selector ie a set of selected elements eg

$('.myelements').someFunction();

If your plugin ends up looking like this (case in point the newly released Microsoft Globalisation plugin)

$.doSomeStuff();

then there isnt much benefit that couldn't be gained from using a closure. However a lot of developers dont understand closures and namespaces in javascript, and by following a plugin development templatethey can get the benefit without fully understanding the pattern.

James Westgate