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 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 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.
$(this).each(...)
Knowing that I can use:
this.each()
will allow me to simplify things a bit.
Maybe as a sign-post that the person doesn't really understand JQuery plugins very well…
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.
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.