article

javascript best practices

WHAT

  1. ] a collection of best practices from the W3C

[WHY]

  1. ]

[WHEN]

  1. ]

[WHERE]

  1. ] READ THE FULL ARTICLE
    1. http://www.w3.org/wiki/JavaScript_best_practices

[EXAMPLE]

  1. Introduction 
    1. ]
  2. Call things by their name — easy, short and readable variable and function names
    1. ]
    2. ] cc namingConventions
  3. Avoid globals 
    1. ]
  4. Stick to a strict coding style
    1. ] goal is consistency - naming conventions, ...
  5. Comment as much as needed but not more 
    1. ] Comments don’t hurt anybody if you do them right, the trick is moderation.
    2. ] Comment when there is an important thing to say, and
    3. ] if you do comment use the /*  */ notation.
    4. ] Single line comments using // can be problematic if people minify your code without stripping comments and in general are less versatile.
    5. ] TIP /* .... // */ - can comment out a code block, then enable it by adding a single slash before the opening /*
  6. Avoid mixing with other technologies 
    1. ] example css - avoid using js to set css attributes where you can, keep css to css, js to js 
  7. Use shortcut notation when it makes sense 
    1. >] example use object literals vs object ?assignment statements?
    2. ] DONT myCar = new Car()
    3. ] myCar.color=red;
    4. ] myCar.type=convertible;
    5. ] myCar....= ... ;
    6. ] DO var myCar = { color:red, type:convertible, ...:...} // avoids writing out myCar. in front of each property
    7. >] example array literals VS array declarations
    8. ] DONT var myCars = new Array()
    9. ] myArray[0] = '1965 Mustang Convertible'
    10. ] myArray[1]= '1966 Mustang 2+2'
    11. ] DO myCars = ['1965 Mustang Convertible, '1966 Mustang 2+2' ]
  8. Modularize — one function per task
    1. ] a general programming bp - create functions that fulfill one job at a time makes it easy for other developers to debug and change your code without having to scan through all the code to work out what code block performs what function.
  9. Enhance progressively
    1. ]
    2. ] cc progressive-enhancemnt
  10. Allow for configuration and translation
  11. Avoid heavy nesting
  12. Optimize loops
  13. Keep DOM access to a minimum
  14. Don’t yield to browser whims
  15. Don’t trust any data
  16. Add functionality with JavaScript, don’t create too much content
  17. Build on the shoulders of giants
  18. Development code is not live code
  19. Summary

[HOW-T0]

  1. ]

[REFERENCE]

Details Photos Edit more

Details

ID: 1692

NAME: EXAMPLE-javascript-best-practices-w3c

DESCRIPTION: LIST of best practices for developing with javascript, by __ @w3c -

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: 0

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and