So I am working on some documentation for a project me and my team
recently completed (is it ever REALLY complete?!) and as the XHTML,
CSS, etc. standards/best practices have been hashed out, I've seen to
only come up with a few real world practices, quite frankly, because
jQuery tends to clean up/simplify a lot of prior issues with
Javascript best practices.
So, I'm requesting some info from the community. If you have a
standard you follow for Javascript and implement it into a best
practice for jQuery, I'd be interested in hearing it.
> So I am working on some documentation for a project me and my team
> recently completed (is it ever REALLY complete?!) and as the XHTML,
> CSS, etc. standards/best practices have been hashed out, I've seen to
> only come up with a few real world practices, quite frankly, because
> jQuery tends to clean up/simplify a lot of prior issues with
> Javascript best practices.
> So, I'm requesting some info from the community. If you have a
> standard you follow for Javascript and implement it into a best
> practice for jQuery, I'd be interested in hearing it.
In no particular order: - Use the var keyword - Cache (don't repeat) queries by throwing the result in a variable - Chain - Use event delegation, especially when you have lots of elements - Write code that reads really nicely, so it doesn't need comments
On Thu, Aug 14, 2008 at 9:21 AM, Joe <joseph.is...@gmail.com> wrote:
> Really? No best practices? Okay.
> On Aug 13, 10:07 am, Joe <joseph.is...@gmail.com> wrote: > > So I am working on some documentation for a project me and my team > > recently completed (is it ever REALLY complete?!) and as the XHTML, > > CSS, etc. standards/best practices have been hashed out, I've seen to > > only come up with a few real world practices, quite frankly, because > > jQuery tends to clean up/simplify a lot of prior issues with > > Javascript best practices.
> > So, I'm requesting some info from the community. If you have a > > standard you follow for Javascript and implement it into a best > > practice for jQuery, I'd be interested in hearing it.
-make plugins -use closures -use "this" - dom plugins return nodes so its chainable
-think with dom nodes -be accessible (no javascript == the website is functionally usable, google can get through ) -think about scale, i.e, w
- dont write too much doc: it gets quickly bloated, unpleasant to read and outdated - talk to eachother in your team so that you harmonize your ways of thinking for naming etc
Richard D. Worth wrote: > In no particular order: > - Use the var keyword > - Cache (don't repeat) queries by throwing the result in a variable > - Chain > - Use event delegation, especially when you have lots of elements > - Write code that reads really nicely, so it doesn't need comments
> - Richard
> On Thu, Aug 14, 2008 at 9:21 AM, Joe <joseph.is...@gmail.com > <mailto:joseph.is...@gmail.com>> wrote:
> Really? No best practices? Okay.
> On Aug 13, 10:07 am, Joe <joseph.is...@gmail.com > <mailto:joseph.is...@gmail.com>> wrote: > > So I am working on some documentation for a project me and my team > > recently completed (is it ever REALLY complete?!) and as the XHTML, > > CSS, etc. standards/best practices have been hashed out, I've > seen to > > only come up with a few real world practices, quite frankly, because > > jQuery tends to clean up/simplify a lot of prior issues with > > Javascript best practices.
> > So, I'm requesting some info from the community. If you have a > > standard you follow for Javascript and implement it into a best > > practice for jQuery, I'd be interested in hearing it.
I'd give it at least a week before I decided there were no best
practices.
I admit though that off the top of my head it's hard to come up with
best practices... it seems to me that jQuery just lends itself to well
structured layout, though I know you can go overboard.
I know for me I hate when chaining is done as:
$('selector').func1
.func2
.func3
...etc
To me it's easier to see $('selector').func1.func2.func3
Beyond that though nothing really jumps out at me. I'd browse through
most of the plugin's source, see how people are writing their plugins
and either a) pick what I like about what folks are doing and create
standards based on that or b) create standards based on what the
"majority" appear to be doing.
I've had to dig into plugins and modify them on a few occasions, and
some are just harder to work with than others.
- unobtrusive scripting (try to do things in a way that visitors
with disabled JavaScript still have functional page)
- use closures
- have your namespace clean (all my global variables and methods
are enclosed into jQuery.extend({...}) , so i invoke them by
$.myCustomMethod(), or $.myGlobalVariable
- since you are working in a team try to make chaining obvious,
so your college doesn't miss a part (every part in new line)
- write short and clear comments
On Aug 14, 11:34 pm, Leanan <CrystalR...@gmail.com> wrote:
> I know for me I hate when chaining is done as:
> $('selector').func1
> .func2
> .func3
> ...etc
I strongly disagree (with a minor modification); I do chaining as:
$('selecector')
.traversal
.action1
.action2
.traversal
.action3
.end
.end
.traversal
.action4
.action5
.end;
This lets the reader easily comprehend which subset of elements are to
be affected by nested actions.
I agree with David in that indented code yields cleaner looking and
easier to read code. My practices:
+ Each function gets a new line.
+ Each sub function indents from the above function.
+ Change the jQuery alias from $() to jq() to make sure no other
libraries interfere with my jQuery and to make the code more clear
that I am calling jQuery functions. To do this in the head of your
site before any other javascript (from jQuery) is called, simply add
this line: var jq = jQuery.noConflict();
+ NO documentation. Clean code reads fine, documentation is prone for
errors as it grows outdated and is never maintained well (in my
experience)
--
Mike
Accidentally sent this to David, sorry about that.
On Aug 15, 4:43 am, erroneousBolock <erroneousBoll...@gmail.com>
wrote:
> So I am working on some documentation for a project me and my team
> recently completed (is it ever REALLY complete?!) and as the XHTML,
> CSS, etc. standards/best practices have been hashed out, I've seen to
> only come up with a few real world practices, quite frankly, because
> jQuery tends to clean up/simplify a lot of prior issues with
> Javascript best practices.
> So, I'm requesting some info from the community. If you have a
> standard you follow for Javascript and implement it into a best
> practice for jQuery, I'd be interested in hearing it.
----- Original Message ----- From: "h3" <hainea...@gmail.com> To: "jQuery (English)" <jquery-en@googlegroups.com> Sent: Friday, August 15, 2008 11:00 AM Subject: [jQuery] Re: Your jQuery Best Practices
== Encapsulation ==
One practice that isn't always used and makes you code really portable and encapsulated:
(function($){ // so inside here we can safely assume that $ really *is* jQuery })(jQuery);
== Use $.extend to specify default options ==
function test(options) var options = $.extend({ width: 500, height: 250 }, options); }
== Hover ==
Instead of user mouseover/mouseout, which will probably get you some quirks anyway, use:
I probably could list more advices but time is lacking .. sorry
On Aug 13, 11:07 am, Joe <joseph.is...@gmail.com> wrote: > So I am working on some documentation for a project me and my team > recently completed (is it ever REALLY complete?!) and as the XHTML, > CSS, etc. standards/best practices have been hashed out, I've seen to > only come up with a few real world practices, quite frankly, because > jQuery tends to clean up/simplify a lot of prior issues with > Javascript best practices.
> So, I'm requesting some info from the community. If you have a > standard you follow for Javascript and implement it into a best > practice for jQuery, I'd be interested in hearing it.
As you can see, hover does nothing more than hook up the 'mouseenter' and 'mouseleave' events to your two handlers. So, if you only need to listen for one of these events and not both, it's perfectly OK to use one of those two events (but not, as you note, mouseover and mouseout).
> // Bad: > $('.elements')[0];
> // Good > $('.elements').get(0);
I must cordially disagree. The jQuery object is specifically designed to walk like a duck and quack like a duck. $(...)[n] is a documented and supported feature, and it's faster than $(...).get(n) too. You're not relying on some kind of internal implementation detail, any more than you would be if you coded:
var a = [ 'a', 'b', 'c' ]; var x = a[0];
The only reason that .get(n) even exists is for historical backward compatibility. In the very first version of jQuery, the $(...) return object was not an array-like object. It had an undocumented property that contained the array of DOM elements, and .get(n) was the only supported way to access that array.
Very early on, we converted the code to make the jQuery object actually be an array-like object with [0], [1], etc. properties. But since there was already code out there using .get(n), it remained for backwards compatibility.
> // Not really bad > $('#element1').addClass('hello').show() > $('#element2').show()
Now that's an interesting one. I hadn't thought of using .add() that way. Essentially you're refactoring out the duplicate calls to .show(). Very clever.
I'm not sure I prefer it in this simple test case, though. I think the "not really bad" example is easier to understand. But I can see where there would be other cases where this trick may help make the code more clear - so thanks for pointing it out!
> > > // Good > > > $('.elements').get(0); > > Why is that bad exactly? Not being disagreeable, just > > want to know. > Hmm. I have no idea why that would be considered bad.
I think I have some sympathy for this point of view. Consider an object-oriented perspective where it's perferred to call an object's methods instead of directly accessing its data, so that you have the freedom to change the object's implementation in the future.
Indeed, jQuery gave up that bit of freedom when the underlying array was exposed directly as [0]..[n] and .length properties on the jQuery object itself, and documented as such. This can no longer be changed without breaking a lot of code.
If you don't like it, blame me: I'm the one who talked John into the change and came up with the initial patch. :-)
As to whether you should use .get(n) or [n] today, I would look at it this way: Even if you use .get(n) in your code, that won't give jQuery the freedom to change its implementation. jQuery will still have to support [n] whether you use it or not. It's a done deal.
So, you may as well use the shorter and more efficient [n].
> As you can see, hover does nothing more than hook up the > 'mouseenter' and > 'mouseleave' events to your two handlers. So, if you only need to > listen for > one of these events and not both, it's perfectly OK to use one of > those two > events (but not, as you note, mouseover and mouseout).
Glad you brought this up, Michael!
A couple things to note about this:
1. The .hover() wasn't always written that way internally. It was a change in one of the 1.2.x versions. So if you're using an old version of jQuery, you might want to double check for those mouseenter and mouseleave events before attempting to use them.
2. At this point there is no shorthand .mouseenter(fn) or .mouseleave(fn) like this is .mouseover(fn) and .mouseout(fn). Instead, we have to use .bind('mouseenter', fn) and .bind('mouseleave', fn)
> > To me it's easier to see $('selector').func1.func2.func3 > From: erroneousBolock > I strongly disagree (with a minor modification); I do chaining as: > $('selecector') > .traversal > .action1 > .action2 > .traversal > .action3 > .end > .end > .traversal > .action4 > .action5 > .end;
> This lets the reader easily comprehend which subset of > elements are to be affected by nested actions.
I agree with both of you. :-)
Leanan, sure, if you have a short chain of functions, by all means put it on one line. But if it gets complex, you may start thinking about breaking it up into smaller chunks - and you're right, this is NOT the way to do it:
What's wrong here? It's trying to line up the dots, and depending on the font you're using, they may or may not line up. In fact, that code sample was obviously written for a proportional font. In a real example, the code would have been written by someone using a typical monospaced font, and the actual spacing would be:
$('selector').func1() .func2() .func3();
If you view that in a monospaced font the periods line up perfectly - but in a proportional font it's ugly.
That's why I don't like this style: I code in a proportional font! If you write this code in a monospaced font, it will look like that last example in my editor. I can read it OK, but who wants to?
If you convert that to erroneous's style, it works fine in any editor, proportional or monospaced:
$('selector') .func1() .func2() .func3();
It doesn't even matter if you indent with spaces or tabs, as long as you use them consistently. It just works.
Again, if they're short method calls just string them together on one line:
$('selector').func1().func2().func3();
But if they're longer and you want to break it into multiple lines for clarity, erroneous's style is the way to do it.
BTW, if you haven't coded in a proportional font, you ought to try it - if your editor supports it. Programmer's editor support for proportional fonts is spotty, but my favorite editor, Komodo [1], handles them fine. Proportional fonts are great - I use Georgia on a high-density display with ClearType enabled, so my code is more readable and I can see more of it at a time.
> > As you can see, hover does nothing more than hook up > > the 'mouseenter' and 'mouseleave' events to your two handlers. > > So, if you only need to listen for one of these events and not both, > > it's perfectly OK to use one of those two events (but not, as you > > note, mouseover and mouseout). > From: Karl Swedberg
> Glad you brought this up, Michael!
> A couple things to note about this:
> 1. The .hover() wasn't always written that way internally. It > was a change in one of the 1.2.x versions. So if you're using > an old version of jQuery, you might want to double check for > those mouseenter and mouseleave events before attempting to use them.
> 2. At this point there is no shorthand .mouseenter(fn) or > .mouseleave(fn) like this is .mouseover(fn) and > .mouseout(fn). Instead, we have to use .bind('mouseenter', > fn) and .bind('mouseleave', fn)
Ah... So I could be way off base in suggesting that the mouseenter and mouseleave events are fair game. I saw that .hover was using them and figure it must be OK! Clearly a case of monkey see, monkey do coding. For all I knew, they could have been intended for internal use only and subject to change. After all, they are not documented.
So what's the intent with these two events? Are they intended for apps to use and will remain stable - in that case they just need to be added to the documentation.
>>> As you can see, hover does nothing more than hook up >>> the 'mouseenter' and 'mouseleave' events to your two handlers. >>> So, if you only need to listen for one of these events and not both, >>> it's perfectly OK to use one of those two events (but not, as you >>> note, mouseover and mouseout).
>> From: Karl Swedberg
>> Glad you brought this up, Michael!
>> A couple things to note about this:
>> 1. The .hover() wasn't always written that way internally. It >> was a change in one of the 1.2.x versions. So if you're using >> an old version of jQuery, you might want to double check for >> those mouseenter and mouseleave events before attempting to use them.
>> 2. At this point there is no shorthand .mouseenter(fn) or >> .mouseleave(fn) like this is .mouseover(fn) and >> .mouseout(fn). Instead, we have to use .bind('mouseenter', >> fn) and .bind('mouseleave', fn)
> Ah... So I could be way off base in suggesting that the mouseenter and > mouseleave events are fair game. I saw that .hover was using them > and figure > it must be OK! Clearly a case of monkey see, monkey do coding. For > all I > knew, they could have been intended for internal use only and > subject to > change. After all, they are not documented.
> So what's the intent with these two events? Are they intended for > apps to > use and will remain stable - in that case they just need to be added > to the > documentation.
> Thanks,
> -Mike
Hey Mike,
I don't think you're off base. It's my impression that the two events are sticking around. But, then again, it's just an impression. And it's just mine.
They actually are in the documentation -- but they're kind of hidden:
They were also mentioned in the Release Notes for jQuery 1.2.2 [1]. I'd love to see them given their own shorthand methods to put them on par with their mousey brethren.
Not sure, but it could be just a matter of adding them to the list around line 2386:
I've been meaning to mention this on the dev list, but it keeps slipping my mind. And I'm a little shy about suggesting such things. Anyway, it's always a pleasure corresponding with you. Any chance at all you can make it to the jQuery Conference next month? Would be great to meet you!
> >>> As you can see, hover does nothing more than hook up
> >>> the 'mouseenter' and 'mouseleave' events to your two handlers.
> >>> So, if you only need to listen for one of these events and not both,
> >>> it's perfectly OK to use one of those two events (but not, as you
> >>> note, mouseover and mouseout).
> >> From: Karl Swedberg
> >> Glad you brought this up, Michael!
> >> A couple things to note about this:
> >> 1. The .hover() wasn't always written that way internally. It
> >> was a change in one of the 1.2.x versions. So if you're using
> >> an old version of jQuery, you might want to double check for
> >> those mouseenter and mouseleave events before attempting to use them.
> >> 2. At this point there is no shorthand .mouseenter(fn) or
> >> .mouseleave(fn) like this is .mouseover(fn) and
> >> .mouseout(fn). Instead, we have to use .bind('mouseenter',
> >> fn) and .bind('mouseleave', fn)
> > Ah... So I could be way off base in suggesting that the mouseenter and
> > mouseleave events are fair game. I saw that .hover was using them
> > and figure
> > it must be OK! Clearly a case of monkey see, monkey do coding. For
> > all I
> > knew, they could have been intended for internal use only and
> > subject to
> > change. After all, they are not documented.
> > So what's the intent with these two events? Are they intended for
> > apps to
> > use and will remain stable - in that case they just need to be added
> > to the
> > documentation.
> > Thanks,
> > -Mike
> Hey Mike,
> I don't think you're off base. It's my impression that the two events
> are sticking around. But, then again, it's just an impression. And
> it's just mine.
> They actually are in the documentation -- but they're kind of hidden:
> They were also mentioned in the Release Notes for jQuery 1.2.2 [1].
> I'd love to see them given their own shorthand methods to put them on
> par with their mousey brethren.
> Not sure, but it could be just a matter of adding them to the list
> around line 2386:
> I've been meaning to mention this on the dev list, but it keeps
> slipping my mind. And I'm a little shy about suggesting such things.
> Anyway, it's always a pleasure corresponding with you. Any chance at
> all you can make it to the jQuery Conference next month? Would be
> great to meet you!
Thanks, Karl, it would be nice to meet you and some of the rest of the gang too.
Alas, I have a high school reunion in Phoenix the same weekend. Great timing!
-Mike
_____
From: Karl Swedberg
Anyway, it's always a pleasure corresponding with you. Any chance at all you can make it to the jQuery Conference next month? Would be great to meet you!