Javascript Guideline
JavaScript File Name Convention
- Use all lowercase filenames. There are some operating systems that are not case sensitive for filenames and using all lowercase prevents inadvertently using two files that differ only in case that might not work on some operating systems.
- Don't use spaces in the filename. While this technically can be made to work there are lots of reasons why spaces in filenames can lead to problems.
- A dash is OK for a word separator. If you want to use some sort of separator for multiple words instead of a space or camelcase as in
various-scripts.js
, a dash is a safe and useful and commonly used separator. - Think about using version numbers in your filenames. When you want to upgrade your scripts, plan for the effects of browser or CDN caching. The simplest way to use long term caching (for speed and efficiency), but immediate and safe upgrades when you upgrade a JS file is to include a version number in the deployed filename or path (like jQuery does with jquery-1.6.2.js) and then you bump/change that version number whenever you upgrade/change the file. This will guarantee that no page that requests the newer version is ever served the older version from a cache.
Coding Practices
- We prefer variables that are declared but not defined to be at the top, followed by variable declarations with definitions
var book, shopping_cart, spa= "Helloworld!", purchase_book = true, tell_friends= true, give_5_star_rating_on_amazon = true, leave_mean_comment = false
Object Literals
var spa= { title: "Single Page WebApplications", //attribute authors: [ "Mike Mikowski", "Josh Powell" ], //array buy_now: function () { //function console.log( "Book ispurchased" ); }