34

What are some good resources to learn best practices for Javascript? I'm mainly concerned about when something should be an object vs. when it should just be tracked in the DOM. Also I would like to better learn how to organize my code so it's easy to unit test.

blake8086
  • 699
  • 8
  • 18
  • [This.](http://stackoverflow.com/questions/39691/javascript-best-practices#39713) –  Sep 02 '08 at 14:59

8 Answers8

36

Seconding Javascript: The Good Parts and Resig's book Secrets of the Javascript Ninja.

Here are some tips for Javascript:

  • Don't pollute the global namespace (put all functions into objects/closures)
    • Take a look at YUI, it's a huge codebase with only 2 global objects: YAHOO and YAHOO_config
  • Use the Module pattern for singletons (http://yuiblog.com/blog/2007/06/12/module-pattern/)
  • Make your JS as reusable as possible (jQuery plugins, YUI modules, basic JS objects.) Don't write tons of global functions.
  • Don't forget to var your variables
  • Use JSlint : http://www.jslint.com/
  • If you need to save state, it's probably best to use objects instead of the DOM.
Ryan Doherty
  • 38,580
  • 4
  • 56
  • 63
  • I like JavaScript: The Good Parts. However, I am not a fan of Crockford's style of coding, I think he's too bent on not making JS look like a typical OO language. I use it just to find out tricks of the language and apply to my own style, which attempts to emulate classical inheritance with JS, since it makes it more readable to people of all language backgrounds. – Ruan Mendes Dec 16 '10 at 00:31
9

I disagree to the "use a framework" statement to some degree. Too many people use frameworks blindly and have little or no understanding of what's going on behind the curtains.

Thomas Kjørnes
  • 1,928
  • 1
  • 17
  • 17
5

I liked JavaScript:The Good Parts by Douglas Crockford although it's focused entirely on the language and ignores the DOM altogether.

Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
slm
  • 861
  • 2
  • 10
  • 12
4

If you don't feel like reading you can watch this video: JavaScript the good parts by Doug Crockford.

Undo
  • 25,519
  • 37
  • 106
  • 129
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
2

Probably the single most important thing is to use a framework, such as jQuery, or prototype, to iron out the differences between browsers, and also make things easier in general.

Cebjyre
  • 6,552
  • 3
  • 32
  • 57
  • 1
    I completely disagree. While at one point this might have been valid advise now it seems frameworks are primarily used by newbs to mask their inexperience and complete inability to write JavaScript or access the DOM. – austincheney Jul 28 '12 at 21:48
  • 1
    I completely disagree with you austincheney. complaining about frameworks is how noobs hide from the fact that they don't know how to use tools to make their job easier. Stop wasting time. Learn how to stand on the shoulders of giants. Also walking the DOM directly is a terrible idea. – akronymn Jan 16 '13 at 01:25
2

YUI Theatre has a bunch of videos (some with transcripts) by Steve Souders, Douglas Crockford, John Resig and others on JavaScript, YUI, website performance and other related topics.

There are also very interested google tech talks on Youtube on jQuery and other frameworks.

dplante
  • 2,445
  • 3
  • 21
  • 27
1

You can pick up a lot from Pro JavaScript Techniques, and I'm looking forward to Resig's forthcoming Secrets of the JavaScript Ninja.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
1

As an addendum to the Crockford book, you may also want to check out this piece Code Conventions for the Javascript Programming Language. I also have a slightly different suggestion: instead of using a JS library off the bat, why not create your own? You may write a crappy library (as I did), but you'll learn something in the process. You have existing examples you can use as models. Also, to help give you an understanding of JS design patterns, I shall recommend another book, 'Pro Javascript Design Patterns'.

bobtorp
  • 31
  • 4