News for hackers. Stuff that matters.
Re:Signs of a poorly written jQuery plugin by NathanKP on Thursday June 03, 2010 @10:02AM (#1401307)
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.

Re:Signs of a poorly written jQuery plugin by twism on Thursday June 03, 2010 @11:44AM (#1401613)
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.
Re:Signs of a poorly written jQuery plugin by benmathes on Thursday June 03, 2010 @05:43PM (#1402682)
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.
Re:Signs of a poorly written jQuery plugin by po on Friday June 04, 2010 @08:10AM (#1404057)
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…

Re:Signs of a poorly written jQuery plugin by vinhboy on Thursday June 03, 2010 @10:58AM (#1401477)
Yea great article. I also had a simliar misconception.
Re:Signs of a poorly written jQuery plugin by naz on Thursday June 03, 2010 @11:53AM (#1401646)
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.
Re:Signs of a poorly written jQuery plugin by adamesque on Thursday June 03, 2010 @07:25PM (#1402960)
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.

Re:Signs of a poorly written jQuery plugin by dhimes on Thursday June 03, 2010 @12:23PM (#1401726)
For the inline return:

    return $(this).each(fn);
I'm actually surprised that the JS engines don't do this kind of optimization automatically.
Re:Signs of a poorly written jQuery plugin by tlrobinson on Thursday June 03, 2010 @01:34PM (#1401921)
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.
Re:Signs of a poorly written jQuery plugin by dhimes on Thursday June 03, 2010 @02:01PM (#1401978)
I see. It speaks to a certain elegance of programming, which is a useful filter for estimating quality. I actually totally get that.
Re:Signs of a poorly written jQuery plugin by apphacker on Thursday June 03, 2010 @05:14PM (#1402584)
#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.
Re:Signs of a poorly written jQuery plugin by WilliamLP on Thursday June 03, 2010 @07:30PM (#1402975)
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.

Re:Signs of a poorly written jQuery plugin by apphacker on Friday June 04, 2010 @05:17PM (#1405687)
I'm going to assume sarcasm since ignoring doesn't usually involve replying.