Friday, October 12, 2007

Seven JavaScript Techniques You Should Be Using Today

link: Digital Web Magazine–Seven JavaScript Techniques You Should Be Using Today

Ramping up my JS, having been in Photoshop mode for the last couple months, here are some techniques I wish I could say that I’ve been using:

  1. Branch when possible – by provide an early return for common cases.
  2. Make Flags – rather than testing for basic browser functionality in each and every function.
  3. Make bridges – to decouple your library functions from browser implementation quirks.
  4. Try Event Delegation – instead of attaching an event to each item in an unordered list, attach one event to the list itself.
  5. Include methods with your getElementsByWhatever – because it’s not often that you get a collection of elements and don’t want to do something to each one. If your getElementsBy… method is already looping through each element, why not do the work then, rather than loop through twice?
  6. Encapsulate your code – to avoid namespace collisions. I’ve started doing this, but look how simple it is to create a closure around your code, even for (especially for?) quick one-off functions.
  7. Reinvent the wheel – even if somebody else has already written a similar widget, maybe you can do better. I’ll add that you’ll certainly learn more writing something from scratch than just hacking somebody else’s code.

No comments: