11

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

Mohammad
  • 7,344
  • 15
  • 48
  • 76
  • 3
    do you have an example of what you're referring to? – Matthew J Morrison Jun 11 '10 at 17:33
  • 1
    Example, please... (the answer is probably: "to allow for call chaining / fluent interface") – Shog9 Jun 11 '10 at 17:34
  • For example like http://plugins.jquery.com/files/jquery.cookie.js.txt or http://code.google.com/p/jquery-json/source/browse/trunk/jquery.json.js or http://stackoverflow.com/questions/3024084/jquery-javascript-how-do-i-convert-a-pixel-value-20px-to-a-number-value-20/3024109#3024109? – Crescent Fresh Jun 11 '10 at 17:35
  • @Shog9 - And don't forget cleanness. – Gert Grenander Jun 11 '10 at 17:36
  • http://plugins.jquery.com/project/base64 this is http://webtoolkit.info/javascript-base64.html – Mohammad Jun 11 '10 at 17:36
  • @Mohammed: ok, yeah... That's kinda pointless. My guess would be, "riding the jQuery coattails" – Shog9 Jun 11 '10 at 17:38
  • oh alright, but when is it ok to do this? – Mohammad Jun 11 '10 at 17:40
  • @Mohammed: when you can actually *use* the jQuery context object to accomplish something. The best use is when your function operates on an element or set of elements - you can then use jQuery to select the elements and call the plugin to perform ** on them (and then continue on chaining, if there's more to do...) – Shog9 Jun 11 '10 at 17:42
  • that seems true, thanks Shog9 – Mohammad Jun 11 '10 at 17:50

3 Answers3

8

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
  • 37,319
  • 5
  • 97
  • 97
  • 3
    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- Jun 11 '10 at 17:41
  • @Harold1983, "handles browser detection", where? ... jQuery is all feature-detection/inference. – James Jun 11 '10 at 18:46
  • 1
    He probably meant internally as in, it's intelligent enough to work across all browsers. – Mohammad Jun 12 '10 at 07:06
4

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
  • 3,454
  • 1
  • 19
  • 24
1

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
  • 11,306
  • 8
  • 61
  • 68