开发者

Is it a good idea to write jQuery plugins just for the sake of neat code?

For example if you write a plugin you could do this $('#myDiv').doAction() instead of doAction($('#myDiv')). and you have to admit, the first one looks more intuitive. Are there开发者_StackOverflow any clear drawbacks of this like performance hits?


You won't see any noticeable performance hit from this, but there is one potential drawback: namespacing your code is very valuable, and makes it easier to maintain (as you probably know). Throwing everything into jQuery.fn may not be the best way to do this.

Personally, I shy away from extending jQuery with very specific things (like app-specific logic or something) but whole-heartedly recommend it for general, DOM-level things (.hasAttr() and .scrollHeight() are two I use a lot). The reason is that there are better places to put app-specific logic (in the module in charge of that area of the app, for example).

A simple heuristic: would it be useful to other people if I made this a public extension? Or is this only relevant to this particular project?

That said, there is no concrete problem with doing this in your code. It's more a semantic issue: is that the best place for it?


Write a jQuery plugin so that you do not need to repeat this over and over again.

There are literally minimal performance issue with this, and mindful that this is at client side, it does not attribute to your server resources.


There aren't any performance hits choosing one approach over the other. It's more of a question of, do you need it as a jquery plugin? I create regular functions if it's a utility function and doesn't need to act upon a jquery object. If it does, then it should be a plugin (no need to pass in the jquery object as a parameter, and you can chain)


You seem to be using jQuery as a framework. It is not one.

Please use a client-side MVC style library like backbone/knockout/sammy/eyeballs for seperations of concerns and structure

Please use an AMD loader like curl/requirejs for modularisation of your code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜