61

I’m currently responsible for rolling out the use of jQuery to the community of Web Developers within our company. Part of this involves presenting a course, however another part involves communicating standards and best practice.

If you Google 'jQuery best practice', you’ll probably find the following among the search results. http://www.smashingmagazine.com/2008/09/16/jquery-examples-and-best-practices/ http://www.artzstudio.com/2009/04/jquery-performance-rules/

These have been helpful and I have gleamed much useful information on them. However, what I would be really interested in would be any tips, traps, opinions, etc, on best practice from experienced jQuery developers and those who may have found themselves in a similar position to myself. Any good links would also be appreciated.

EDIT:

Added a jQuery Coding Standards section on my own page:

http://www.jameswiseman.com/blog/?p=48

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
James Wiseman
  • 29,946
  • 17
  • 95
  • 158

4 Answers4

42

You can find this trending topic right here in StackOverflow.com

jQuery pitfalls to avoid

Very interesting useful tips one after the other.

here are some more i found in my bookmarks:

Community
  • 1
  • 1
adardesign
  • 33,973
  • 15
  • 62
  • 84
  • 2
    Interesting links, thanks. I realise the topic is quite similar, however I'm still interested in the responses from the perspective of those in a similar position to myself. Also, the title doesn't lend itself to a usful link when searching, whereas this over time may well pop up (and refernce the other topic now you've added it in!) – James Wiseman Aug 07 '09 at 16:22
  • 1
    btw, I just found a interesting link (a jQuery experiment playground) commadot.com/jquery if you go link after link you can learn a lot... – adardesign Dec 21 '09 at 17:15
  • One more link http://lab.abhinayrathore.com/jquery-standards/ – FDisk Feb 01 '16 at 15:07
27

Something I've personally started to do is a sort of an Apps Hungarian Notation for jQuery sets, by prefixing those variables with a $

var someInt = 1;
var $someQueryCollection = $( 'selector' );

I find that as my jQuery snippets grow, this becomes invaluable, not only in the promotion of storing jQuery sets as variables, but to help me keep track of which variables actually are jQuery sets.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
12

Unobtrusive JavaScript (separation of markup and behavior)

Back in the days, it was common to put your click handler inside the markup. Now it's recommended that you do not write your JS code inside your markup but include it via DOM events.

Progressive enhancement

User gets better experience if they are using standards compliant browser and/or has JavaScript enabled. Website/web application is still accessible even if they have older browser or has JS disabled.

Feature detection and not browser detection

Keeping above points aside, I would really focus on conveying the message (breaking the pre-conceived notion) that JavaScript is a toy language. I have seen too many developers who thinks this way and everything is downhill from there. You need to explain them how JavaScript is a very powerful language and why they need a JS library (because of browser inconsistencies) even though JS itself is very powerful.

Good luck.

Andrew
  • 227,796
  • 193
  • 515
  • 708
SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
  • 1
    Jason, excuse me but could you point out where did I write that jQuery works differently than JavaScript? In fact, I didn't even mention jQuery in my answer. – SolutionYogi Aug 07 '09 at 18:54
  • Do you really think that "JavaScript is a toy language" or is that a typo? – Lie Ryan Mar 31 '13 at 04:18
2

The way that jQuery works is NOT the same way that JavaScript works, even though they are one and the same. jQuery works on CSS selectors, like the class name(s) and the id of elements. To select an item in jQuery, you do:

$("#yourID") or $(".yourClass") or $("div") or $("#yourID p") etc

And you will get a collection of all the elements on the page that fit your criteria. You can then perform your actions on ALL of those elements without any looping of any sort. This is important to remember:

$(".yourClass").click(function(){
    //do stuff
});

will affect all items with .yourClass attached to them. One tip: if you're going to access the $(this), you should save it as a local variable:

$(".yourClass").click(function(){
    var $this = $(this);
});

as it will speed up your function.

Jason
  • 51,583
  • 38
  • 133
  • 185
  • 17
    -1. I don't like this statement 'The way that jQuery works is NOT the same way that JavaScript works'. jQuery is JavaScript. I would word it as 'There is a difference between vanilla JS code for DOM manipulation vs jQuery JS code for DOM manipulation.' – SolutionYogi Aug 07 '09 at 17:00
  • 3
    ....except it it's true. i mentioned they are one and the same, but jQuery abstracts a lot of the crap you're required to do w/javascript. so while you can mix js in w/jQ, they do NOT work the same, and a lot of JS devs try to write JQ code the same way they write JS code. – Jason Aug 07 '09 at 17:16
  • 6
    I thoroughly understand how jQuery and JavaScript works. I have objection with how the sentence is phrased, IMHO it doesn't read right. – SolutionYogi Aug 07 '09 at 17:23