Skip to content

Instantly share code, notes, and snippets.

@sukhmeet2390
Created August 5, 2013 07:46
Show Gist options
  • Save sukhmeet2390/6154111 to your computer and use it in GitHub Desktop.
Save sukhmeet2390/6154111 to your computer and use it in GitHub Desktop.
Javascript coding style
Rule 1: use var . (avoid globals)
Rule 2: Use THIS_IS_A_CONSTANT
Rule 3: Use Semicolons and the end of variable and function expressions ( and not function declarations)
Rule 4 : function declarations within blocks
//DONT if(x){
function foo()
}
// DO if(x){
var foo = function(){}
}
Rule 4 :standards are preferred
string.charAt(3) is preferred than string[3]
Rule 5: attaching methods and properties use prototype .
Foo.prototype.bar = function(){ ..... }
Rule 6 : to remove the property use 'null'
this.property = null is preferred over delete this.property;
coz : In modern JavaScript engines, changing the number of properties on an object is much slower than reassigning the values.
Rule 7 : Think twice when using closures as they can cause memory leaks
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Closures#Closures
Rule 8 : Avoid Evals
/TODO/
Rule 9 : use for-in loop only for iterating over objects and hash maps
In arrays its wrong
eg: var a = [ 1, 3, 4 ];
a.foo = 'wine';
for ( var key in a ) console.log(a[key]);
Rule 10 : Do not use multiline string literals instead use concat operation
Rule 11 : use Array and object literals instead of Array and object constructors
Rule 12 : Avoid modifying builtin prototypes. They are DANGEROUS
Rule 13 : In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, CONSTANT_VALUES_LIKE_THIS, foo.namespaceNamesLikeThis.bar, and filenameslikethis.js.
Private properties and methods should be named with a trailing underscore.
Protected properties and methods should be named without a trailing underscore (like public ones).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment