开发者

Is there any benefit to defining a utility function directly on the jQuery object?

Are there any specific benefits derived from defining a utility function directly on the jQuery object:

For instance, given the following two constructs:

$.someUtility = function(){
   //do some something with jQuery
}


var someUtility = function(){
   //do some something with jQuery
}

Is there any specific reason I would want to use the first example over the second?

Quick Update: I don't need to do any chaining, and my u开发者_开发技巧tility is not a plugin in the traditional sense; It will not perform any operations on a jQuery selector.


You're simply borrowing the global jQuery function object to make your function available to other scripts without further polluting the global variable environment.

If you have no other scripts that rely on that function, you could make it a local variable with no disadvantage except that you'd be polluting your local variable environment.


If you're strictly writing a utility function as opposed to a wrapper method I think the main benefit would simply be that your coding style would be more consistent. A consistent style could be important to you if you plan on using the function on more than a single page.


Three good reasons can be found for choosing to append functions directly on the JQuery object:

  • you want to build a JQuery plugin (most obvious reason)

  • the function you are programming applies to DOM nodes, so it could be directly applied to the DOM nodes returned by a JQuery query (sorry for the rhyme)

  • to keep consistency throughout your code, as you will be lead to use the $ object


Use $.fn.someUtility.

Basically, the first option is creating a jQuery plugin. This allows for your code to be easily reusable in the jQuery context. Inside a jQuery plugin, the this identifier points to the element that was selected in the jQuery selector, allowing for more flexibility when manipulating elements.

I guess the answer to your question is that it really depends. Are you using your function in multiple places?

See JQuery Plugins for more information.


The reason is that your function can then operate on arrays of jQuery-wrapped objects without jumping through the usual hoops. Also in the context of your function, this becomes a reference to the jQuery object on which your function was invoked.

EDIT

My answer assumed defining your function under $.fn.func.

Unless you require access to jQuery selected elements, the only benefit I can see would be by defining under $.func is that you avoid name collisions with other functions defined in the global scope. (As mentioned in my comment below.)


If your utility function uses jQuery (and thus requires it's presence), then I'd say you can use their namespace and put it under $.fn.myUtility as others have recommended. If your utility function is entirely independent of jQuery and does not use it at all and you are likely to have other such utility functions to go with it, then I would tend to make my own global namespace and put your function there such as JLF.myUtility. This makes your code a little easier to reuse in the future even in non-jQuery project.


For me, jQuery gets a lot of it's flexibility and power from the "daisy chain" setup, since every plugin returns "this" you can always call another plugin immediately after, not to mention you are extending an already powerful tool, and that seems like a good idea:)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜