Hacker News new | past | comments | ask | show | jobs | submit login
Signs of a poorly written jQuery plugin (remysharp.com)
60 points by jimsteinhart on June 3, 2010 | hide | past | favorite | 13 comments



His second point, "Double jQuery" is because of inconstancy in jQuery itself. For all event callbacks 'this' is the relevant DOM node, but for plugin functions 'this' is a jQuery object. It is understandable that a plugin author would assume that it is a DOM node and since calling $(this) would just return itself they have no way of knowing. I wouldn't say it reflects on the plugin author at all.


This article helped me a lot. I had always thought that it had to be written as:

$(this).each(...)

Knowing that I can use:

this.each()

will allow me to simplify things a bit.


In the context of plugin code, "this" is a jQuery object. In event callbacks or operating on DOM elements, "this" is a plain old DOM object.


Saying $(this).somejQueryFunction() is defensive programming, it ensures that your object gets jQuery'd. If it's already a jQuery object, it's not expensive to do. This is generic advice for all the various kinds of 'this' that you'll encounter in javascript, not the specific case mentioned in the linked article.


It does seem a bit nit-picky. I would expect that right away in the JQuery call tests if it's already a JQuery object and short-circuits. It would save you one call and it's not exactly in a tight loop.

Maybe as a sign-post that the person doesn't really understand JQuery plugins very well…


Yea great article. I also had a simliar misconception.


It's important to keep in mind that these are literally what the title makes them out to be: simple signposts that indicate poor code might lie within.

I probably wouldn't immediately toss out a plugin that exhibited one or more of these signs, but I would take it as a sign that I ought to do a slightly more thorough skimming of the code before using.

It's a shame that the jQuery plugin directory doesn't make separating the good from the bad much easier.


For the inline return:

    return $(this).each(fn);
I'm actually surprised that the JS engines don't do this kind of optimization automatically.


It's quite possible some of them do, but the performance difference is probably negligible anyway, it's just extra code that doesn't need to exist.


I see. It speaks to a certain elegance of programming, which is a useful filter for estimating quality. I actually totally get that.


#1. I hate the "this" keyword abuse in jQuery, this article demonstrates it clearly. #2. Some minor code style issues do not make a bad plugin.


By "this article", do you mean the article in question, or was that a self-reference to your own comment?

Also I ignored what you were saying because your use of a temporary placeholder in your first sentence was a red flag, indicative of poor writing. Better would have been "This article demonstrates clearly why I hate abuse of the "this" keyword in jQuery", eliminating the extra "it" altogether.


I'm going to assume sarcasm since ignoring doesn't usually involve replying.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: