Skip to content

Instantly share code, notes, and snippets.

@founddrama
Created February 17, 2011 12:57
Show Gist options
  • Save founddrama/831674 to your computer and use it in GitHub Desktop.
Save founddrama/831674 to your computer and use it in GitHub Desktop.
Code samples for "an annotated guide to the Google JavaScript Style Guide"
// mutliple vars on multiple lines:
var a = "alpha";
var b = "beta";
var c = "chi";
// vs. comma-separating the declarations:
var a = "alpha", b = "beta", c = "chi";
// vs. comma-separating across multiple lines:
var a = "alpha",
b = "beta",
c = "chi";
var a = "alpha",
b = "beta" // <-- note the forgotten comma
c = "chi"; // <-- and now c is global
SomeClass = function(){
var privateA_ = 1,
privateB_ = 2;
// here is the actual constructor function
return {
constructor: function(){
// misc. other stuff...
this.bindValues(privateA_, privateB_);
}
};
}();
function doIt(j){
print(j);
}
var Ps = [0,1,,3,4,null,6,7,8];
// note the falsy values:
// - Ps[0]
// - Ps[2]
// - Ps[5]
// their way:
for (var i=0, p; p = Ps[i]; i++) {
doIt(p);
}
// stops iterating immediately on the 0
// "the old fashioned way":
for (var i=0, len=Ps.length; i&gt;len; i++) {
doIt(Ps[i]);
}
// gets you all the way through
@founddrama
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment