JS Best Practices

by Ivan Gerasimenko

HTML

<!-- http://www.w3.org/wiki/JavaScript_best_practices -->

JavaScript

//===========================================================
    // Separate var statements:

var a = 1;
var b = 2;

var foo = {
    prop: 123
};
var bar = {
    prop: 456
};

//put multiple comma-separated declarations in a single var statement, but only if they don’t have assignments
var foo, bar, baz;

//===========================================================
    // Do use semicolons at the end of statements

// cause: please don’t intentionally make your code unreadable; I guarantee it will be unreadable enough without any extra help

//===========================================================
    // console log breaks scripts in IE (if dev-panel is blocked/closed)
  if(typeof console === "undefined"){
    console = { log: function() { } }; // dummy console.log()
  }
  console.log("doc ready");

// read more: easy-to-use, cross-browser logging solution by Ben Alman
// "JavaScript Debug: A simple wrapper for console.log" ba-debug.js

//===========================================================
    // 

//===========================================================
    // Be careful with "new Date(...)"
var d = new Date("February 31, 2014 00:00:00");  // => Mon Mar 03 2014 00:00:00
var d = new Date("February 32, 2014 00:00:00");  // => Invalid Date 

var dat = new Date(2014, 01, 31); // => Mon Mar 03 2014 00:00:00
var dat = new Date(2014, 01, 32); // => Mon Mar 04 2014 00:00:00
var dat = new Date(2014, 01, 302);// => Sat Nov 29 2014 00:00:00

//===========================================================
    // Allow for configuration and translation
//???????????????????? try it sometime http://www.w3.org/wiki/JavaScript_best_practices

//===========================================================
    // Modularize - one function per task
    // So do use Facade ( function init() { calls all the other stuff } )
    // Function should have one way in & one way out
    // Use & create Helper...