From 1d0443c6374960392979bb2087c558c39379677b Mon Sep 17 00:00:00 2001 From: mlarcher Date: Tue, 8 Jan 2013 11:44:46 +0100 Subject: [PATCH 1/8] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6c87267cce..9452e0c41b 100644 --- a/README.md +++ b/README.md @@ -436,11 +436,13 @@ // good function() { + var name; + if (!arguments.length) { return false; } - var name = getName(); + name = getName(); return true; } From 7e39982b189d68487c4e233c573de2a791b76573 Mon Sep 17 00:00:00 2001 From: mlarcher Date: Tue, 8 Jan 2013 11:48:20 +0100 Subject: [PATCH 2/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9452e0c41b..90b0cdc1ff 100644 --- a/README.md +++ b/README.md @@ -592,14 +592,14 @@ ## Blocks - - Use braces with all multi-line blocks. + - Always use braces ```javascript // bad if (test) return false; - // good + // bad if (test) return false; // good From 63d8d335ec22be1a196437daab70bbebb56d0154 Mon Sep 17 00:00:00 2001 From: mlarcher Date: Tue, 8 Jan 2013 11:59:39 +0100 Subject: [PATCH 3/8] Update README.md --- README.md | 514 +++++++++++++++++++++++++++--------------------------- 1 file changed, 257 insertions(+), 257 deletions(-) diff --git a/README.md b/README.md index 90b0cdc1ff..681a662732 100644 --- a/README.md +++ b/README.md @@ -86,16 +86,16 @@ ```javascript // bad var superman = { - class: 'superhero', - default: { clark: 'kent' }, - private: true + class: 'superhero', + default: { clark: 'kent' }, + private: true }; // good var superman = { - klass: 'superhero', - defaults: { clark: 'kent' }, - hidden: true + klass: 'superhero', + defaults: { clark: 'kent' }, + hidden: true }; ``` **[[⬆]](#TOC)** @@ -120,12 +120,12 @@ // bad for (i = 0; i < 100; i++) { - hundredOdds.push(i * 2 + 1); + hundredOdds.push(i * 2 + 1); } // good for (i = 0; i < 100; i++) { - hundredOdds[i] = i * 2 + 1; + hundredOdds[i] = i * 2 + 1; } ``` @@ -151,7 +151,7 @@ // bad for (i = 0; i < len; i++) { - itemsCopy[i] = items[i]; + itemsCopy[i] = items[i]; } // good @@ -196,11 +196,11 @@ // good var errorMessage = 'This is a super long error that ' + - 'was thrown because of Batman.' + - 'When you stop to think about ' + - 'how Batman had anything to do ' + - 'with this, you would get nowhere ' + - 'fast.'; + 'was thrown because of Batman.' + + 'When you stop to think about ' + + 'how Batman had anything to do ' + + 'with this, you would get nowhere ' + + 'fast.'; ``` - When programatically building up a string, use Array#join instead of string concatenation. Mostly for IE: [jsPerf](http://jsperf.com/string-vs-array-concat/2). @@ -225,24 +225,24 @@ // bad function inbox(messages) { - items = '
    '; - - for (i = 0; i < length; i++) { - items += '
  • ' + messages[i].message + '
  • '; - } - - return items + '
'; + items = '
    '; + + for (i = 0; i < length; i++) { + items += '
  • ' + messages[i].message + '
  • '; + } + + return items + '
'; } // good function inbox(messages) { - items = []; - - for (i = 0; i < length; i++) { - items[i] = messages[i].message; - } - - return '
  • ' + items.join('
  • ') + '
'; + items = []; + + for (i = 0; i < length; i++) { + items[i] = messages[i].message; + } + + return '
  • ' + items.join('
  • ') + '
'; } ``` @@ -256,17 +256,17 @@ ```javascript // anonymous function expression var anonymous = function() { - return true; + return true; }; // named function expression var named = function named() { - return true; + return true; }; // immediately-invoked function expression (IIFE) (function() { - console.log('Welcome to the Internet. Please follow me.'); + console.log('Welcome to the Internet. Please follow me.'); })(); ``` @@ -275,16 +275,16 @@ ```javascript // bad if (currentUser) { - function test() { - console.log('Nope.'); - } + function test() { + console.log('Nope.'); + } } // good if (currentUser) { - var test = function test() { - console.log('Yup.'); - }; + var test = function test() { + console.log('Yup.'); + }; } ``` @@ -293,12 +293,12 @@ ```javascript // bad function nope(name, options, arguments) { - // ...stuff... + // ...stuff... } // good function yup(name, options, args) { - // ...stuff... + // ...stuff... } ``` @@ -312,8 +312,8 @@ ```javascript var luke = { - jedi: true, - age: 28 + jedi: true, + age: 28 }; // bad @@ -327,12 +327,12 @@ ```javascript var luke = { - jedi: true, - age: 28 + jedi: true, + age: 28 }; function getProp(prop) { - return luke[prop]; + return luke[prop]; } var isJedi = getProp('jedi'); @@ -393,58 +393,58 @@ ```javascript // bad function() { - test(); - console.log('doing stuff..'); - - //..other stuff.. - - var name = getName(); - - if (name === 'test') { - return false; - } - - return name; + test(); + console.log('doing stuff..'); + + //..other stuff.. + + var name = getName(); + + if (name === 'test') { + return false; + } + + return name; } // good function() { - var name = getName(); - - test(); - console.log('doing stuff..'); - - //..other stuff.. - - if (name === 'test') { - return false; - } - - return name; + var name = getName(); + + test(); + console.log('doing stuff..'); + + //..other stuff.. + + if (name === 'test') { + return false; + } + + return name; } // bad function() { - var name = getName(); - - if (!arguments.length) { - return false; - } - - return true; + var name = getName(); + + if (!arguments.length) { + return false; + } + + return true; } // good function() { - var name; + var name; - if (!arguments.length) { - return false; - } - - name = getName(); - - return true; + if (!arguments.length) { + return false; + } + + name = getName(); + + return true; } ``` @@ -459,7 +459,7 @@ // we know this wouldn't work (assuming there // is no notDefined global variable) function example() { - console.log(notDefined); // => throws a ReferenceError + console.log(notDefined); // => throws a ReferenceError } // creating a variable declaration after you @@ -467,17 +467,17 @@ // variable hoisting. Note: the assignment // value of `true` is not hoisted. function example() { - console.log(declaredButNotAssigned); // => undefined - var declaredButNotAssigned = true; + console.log(declaredButNotAssigned); // => undefined + var declaredButNotAssigned = true; } // The interpretor is hoisting the variable // declaration to the top of the scope. // Which means our example could be rewritten as: function example() { - var declaredButNotAssigned; - console.log(declaredButNotAssigned); // => undefined - declaredButNotAssigned = true; + var declaredButNotAssigned; + console.log(declaredButNotAssigned); // => undefined + declaredButNotAssigned = true; } ``` @@ -485,13 +485,13 @@ ```javascript function example() { - console.log(anonymous); // => undefined - - anonymous(); // => TypeError anonymous is not a function - - var anonymous = function() { - console.log('anonymous function expression'); - }; + console.log(anonymous); // => undefined + + anonymous(); // => TypeError anonymous is not a function + + var anonymous = function() { + console.log('anonymous function expression'); + }; } ``` @@ -499,28 +499,28 @@ ```javascript function example() { - console.log(named); // => undefined - - named(); // => TypeError named is not a function - - superPower(); // => ReferenceError superPower is not defined - - var named = function superPower() { - console.log('Flying'); - }; - - - // the same is true when the function name - // is the same as the variable name. - function example() { console.log(named); // => undefined - + named(); // => TypeError named is not a function - - var named = function named() { - console.log('named'); + + superPower(); // => ReferenceError superPower is not defined + + var named = function superPower() { + console.log('Flying'); }; - } + + + // the same is true when the function name + // is the same as the variable name. + function example() { + console.log(named); // => undefined + + named(); // => TypeError named is not a function + + var named = function named() { + console.log('named'); + }; + } } ``` @@ -528,11 +528,11 @@ ```javascript function example() { - superPower(); // => Flying - - function superPower() { - console.log('Flying'); - } + superPower(); // => Flying + + function superPower() { + console.log('Flying'); + } } ``` @@ -556,8 +556,8 @@ ```javascript if ([0]) { - // true - // An array is an object, objects evaluate to true + // true + // An array is an object, objects evaluate to true } ``` @@ -566,22 +566,22 @@ ```javascript // bad if (name !== '') { - // ...stuff... + // ...stuff... } // good if (name) { - // ...stuff... + // ...stuff... } // bad if (collection.length > 0) { - // ...stuff... + // ...stuff... } // good if (collection.length) { - // ...stuff... + // ...stuff... } ``` @@ -597,14 +597,14 @@ ```javascript // bad if (test) - return false; + return false; // bad if (test) return false; // good if (test) { - return false; + return false; } // bad @@ -612,7 +612,7 @@ // good function() { - return false; + return false; } ``` @@ -632,9 +632,9 @@ // @return element function make(tag) { - // ...stuff... - - return element; + // ...stuff... + + return element; } // good @@ -647,9 +647,9 @@ */ function make(tag) { - // ...stuff... - - return element; + // ...stuff... + + return element; } ``` @@ -665,21 +665,21 @@ // bad function getType() { - console.log('fetching type...'); - // set the default type to 'no type' - var type = this._type || 'no type'; - - return type; + console.log('fetching type...'); + // set the default type to 'no type' + var type = this._type || 'no type'; + + return type; } // good function getType() { - console.log('fetching type...'); - - // set the default type to 'no type' - var type = this._type || 'no type'; - - return type; + console.log('fetching type...'); + + // set the default type to 'no type' + var type = this._type || 'no type'; + + return type; } ``` @@ -688,12 +688,12 @@ ## Whitespace - - Use soft tabs set to 2 spaces + - Use soft tabs set to 4 spaces ```javascript // bad function() { - ∙∙∙∙var name; + ∙∙var name; } // bad @@ -703,7 +703,7 @@ // good function() { - ∙∙var name; + ∙∙∙∙var name; } ``` - Place 1 space before the leading brace. @@ -711,24 +711,24 @@ ```javascript // bad function test(){ - console.log('test'); + console.log('test'); } // good function test() { - console.log('test'); + console.log('test'); } // bad dog.set('attr',{ - age: '1 year', - breed: 'Bernese Mountain Dog' + age: '1 year', + breed: 'Bernese Mountain Dog' }); // good dog.set('attr', { - age: '1 year', - breed: 'Bernese Mountain Dog' + age: '1 year', + breed: 'Bernese Mountain Dog' }); ``` - Place an empty newline at the end of the file. @@ -736,14 +736,14 @@ ```javascript // bad (function(global) { - // ...stuff... + // ...stuff... })(this); ``` ```javascript // good (function(global) { - // ...stuff... + // ...stuff... })(this); ``` @@ -758,11 +758,11 @@ // good $('#items') - .find('.selected') - .highlight() - .end() - .find('.open') - .updateCount(); + .find('.selected') + .highlight() + .end() + .find('.open') + .updateCount(); // bad var leds = stage.selectAll('.led').data(data).enter().append("svg:svg").class('led', true) @@ -772,13 +772,13 @@ // good var leds = stage.selectAll('.led') - .data(data) + .data(data) .enter().append("svg:svg") - .class('led', true) - .attr('width', (radius + margin) * 2) + .class('led', true) + .attr('width', (radius + margin) * 2) .append("svg:g") - .attr("transform", "translate(" + (radius + margin) + "," + (radius + margin) + ")") - .call(tron.led); + .attr("transform", "translate(" + (radius + margin) + "," + (radius + margin) + ")") + .call(tron.led); ``` ## Leading Commas @@ -806,10 +806,10 @@ // good var hero = { - firstName: 'Bob', - lastName: 'Parr', - heroName: 'Mr. Incredible', - superPower: 'strength' + firstName: 'Bob', + lastName: 'Parr', + heroName: 'Mr. Incredible', + superPower: 'strength' }; ``` @@ -823,20 +823,20 @@ ```javascript // bad (function() { - var name = 'Skywalker' - return name + var name = 'Skywalker' + return name })() // good (function() { - var name = 'Skywalker'; - return name; + var name = 'Skywalker'; + return name; })(); // good ;(function() { - var name = 'Skywalker'; - return name; + var name = 'Skywalker'; + return name; })(); ``` @@ -922,12 +922,12 @@ ```javascript // bad function q() { - // ...stuff... + // ...stuff... } // good function query() { - // ..stuff.. + // ..stuff.. } ``` @@ -940,14 +940,14 @@ var this-is-my-object = {}; function c() {}; var u = new user({ - name: 'Bob Parr' + name: 'Bob Parr' }); // good var thisIsMyObject = {}; function thisIsMyFunction() {}; var user = new User({ - name: 'Bob Parr' + name: 'Bob Parr' }); ``` @@ -956,20 +956,20 @@ ```javascript // bad function user(options) { - this.name = options.name; + this.name = options.name; } var bad = new user({ - name: 'nope' + name: 'nope' }); // good function User(options) { - this.name = options.name; + this.name = options.name; } var good = new User({ - name: 'yup' + name: 'yup' }); ``` @@ -989,26 +989,26 @@ ```javascript // bad function() { - var self = this; - return function() { - console.log(self); - }; + var self = this; + return function() { + console.log(self); + }; } // bad function() { - var that = this; - return function() { - console.log(that); - }; + var that = this; + return function() { + console.log(that); + }; } // good function() { - var _this = this; - return function() { - console.log(_this); - }; + var _this = this; + return function() { + console.log(_this); + }; } ``` @@ -1017,12 +1017,12 @@ ```javascript // bad var log = function(msg) { - console.log(msg); + console.log(msg); }; // good var log = function log(msg) { - console.log(msg); + console.log(msg); }; ``` @@ -1053,12 +1053,12 @@ ```javascript // bad if (!dragon.age()) { - return false; + return false; } // good if (!dragon.hasAge()) { - return false; + return false; } ``` @@ -1066,17 +1066,17 @@ ```javascript function Jedi(options) { - options || (options = {}); - var lightsaber = options.lightsaber || 'blue'; - this.set('lightsaber', lightsaber); + options || (options = {}); + var lightsaber = options.lightsaber || 'blue'; + this.set('lightsaber', lightsaber); } Jedi.prototype.set = function(key, val) { - this[key] = val; + this[key] = val; }; Jedi.prototype.get = function(key) { - return this[key]; + return this[key]; }; ``` @@ -1089,27 +1089,27 @@ ```javascript function Jedi() { - console.log('new jedi'); + console.log('new jedi'); } // bad Jedi.prototype = { - fight: function fight() { - console.log('fighting'); - }, - - block: function block() { - console.log('blocking'); - } + fight: function fight() { + console.log('fighting'); + }, + + block: function block() { + console.log('blocking'); + } }; // good Jedi.prototype.fight = function fight() { - console.log('fighting'); + console.log('fighting'); }; Jedi.prototype.block = function block() { - console.log('blocking'); + console.log('blocking'); }; ``` @@ -1118,12 +1118,12 @@ ```javascript // bad Jedi.prototype.jump = function() { - this.jumping = true; - return true; + this.jumping = true; + return true; }; Jedi.prototype.setHeight = function(height) { - this.height = height; + this.height = height; }; var luke = new Jedi(); @@ -1132,19 +1132,19 @@ // good Jedi.prototype.jump = function() { - this.jumping = true; - return this; + this.jumping = true; + return this; }; Jedi.prototype.setHeight = function(height) { - this.height = height; - return this; + this.height = height; + return this; }; var luke = new Jedi(); luke.jump() - .setHeight(20); + .setHeight(20); ``` @@ -1152,16 +1152,16 @@ ```javascript function Jedi(options) { - options || (options = {}); - this.name = options.name || 'no name'; + options || (options = {}); + this.name = options.name || 'no name'; } Jedi.prototype.getName = function getName() { - return this.name; + return this.name; }; Jedi.prototype.toString = function toString() { - return 'Jedi - ' + this.getName(); + return 'Jedi - ' + this.getName(); }; ``` @@ -1179,19 +1179,19 @@ // fancyInput/fancyInput.js !function(global) { - 'use strict'; - - var previousFancyInput = global.FancyInput; - - function FancyInput(options) { - this.options = options || {}; - } - - FancyInput.noConflict = function noConflict() { - global.FancyInput = previousFancyInput; - }; - - global.FancyInput = FancyInput; + 'use strict'; + + var previousFancyInput = global.FancyInput; + + function FancyInput(options) { + this.options = options || {}; + } + + FancyInput.noConflict = function noConflict() { + global.FancyInput = previousFancyInput; + }; + + global.FancyInput = FancyInput; }(this); ``` @@ -1215,25 +1215,25 @@ ```javascript // bad function setSidebar() { - $('.sidebar').hide(); - - // ...stuff... - - $('.sidebar').css({ - 'background-color': 'pink' - }); + $('.sidebar').hide(); + + // ...stuff... + + $('.sidebar').css({ + 'background-color': 'pink' + }); } // good function setSidebar() { - var $sidebar = $('.sidebar'); - $sidebar.hide(); - - // ...stuff... - - $sidebar.css({ - 'background-color': 'pink' - }); + var $sidebar = $('.sidebar'); + $sidebar.hide(); + + // ...stuff... + + $sidebar.css({ + 'background-color': 'pink' + }); } ``` @@ -1276,7 +1276,7 @@ ```javascript function() { - return true; + return true; } ``` From ac6443c76433bd12926f9d4abed017656682f65b Mon Sep 17 00:00:00 2001 From: mlarcher Date: Tue, 8 Jan 2013 12:07:40 +0100 Subject: [PATCH 4/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 681a662732..cf43d4da9f 100644 --- a/README.md +++ b/README.md @@ -1113,7 +1113,7 @@ }; ``` - - Methods can return `this` to help with method chaining. + - Methods should return `this` to help with method chaining. ```javascript // bad From 74a07e18399d84a059037cba80d34ec5c4aa3718 Mon Sep 17 00:00:00 2001 From: mlarcher Date: Tue, 8 Jan 2013 12:09:28 +0100 Subject: [PATCH 5/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cf43d4da9f..b38d1392af 100644 --- a/README.md +++ b/README.md @@ -1113,7 +1113,7 @@ }; ``` - - Methods should return `this` to help with method chaining. + - Methods that have no expected return value should return `this` to allow for method chaining. ```javascript // bad From cc7abf8035a652a9dc24c1d3b49982416e236aa4 Mon Sep 17 00:00:00 2001 From: mlarcher Date: Tue, 8 Jan 2013 12:11:23 +0100 Subject: [PATCH 6/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b38d1392af..afd2e80d24 100644 --- a/README.md +++ b/README.md @@ -1170,7 +1170,7 @@ ## Modules - - The module should start with a `!`. This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated. + - The module should start with a `;`. This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated. - The file should be named with camelCase, live in a folder with the same name, and match the name of the single export. - Add a method called noConflict() that sets the exported module to the previous version. - Always declare `'use strict';` at the top of the module. @@ -1178,7 +1178,7 @@ ```javascript // fancyInput/fancyInput.js - !function(global) { + ;function(global) { 'use strict'; var previousFancyInput = global.FancyInput; From a9b895f8f816bc39be20087074daae975f1de3e3 Mon Sep 17 00:00:00 2001 From: mlarcher Date: Tue, 8 Jan 2013 13:41:01 +0100 Subject: [PATCH 7/8] Update README.md --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index afd2e80d24..6e38a43ce7 100644 --- a/README.md +++ b/README.md @@ -1253,11 +1253,8 @@ // good $('.sidebar > ul').hide(); - // good (slower) + // good $sidebar.find('ul'); - - // good (faster) - $($sidebar[0]).find('ul'); ``` **[[⬆]](#TOC)** From 725809e8b63f0e960d50578a4902ee4361b1d779 Mon Sep 17 00:00:00 2001 From: mlarcher Date: Tue, 8 Jan 2013 14:12:48 +0100 Subject: [PATCH 8/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6e38a43ce7..7a4ffa568a 100644 --- a/README.md +++ b/README.md @@ -1170,7 +1170,7 @@ ## Modules - - The module should start with a `;`. This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated. + - The module should start with a `!`. This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated. - The file should be named with camelCase, live in a folder with the same name, and match the name of the single export. - Add a method called noConflict() that sets the exported module to the previous version. - Always declare `'use strict';` at the top of the module. @@ -1178,7 +1178,7 @@ ```javascript // fancyInput/fancyInput.js - ;function(global) { + !function(global) { 'use strict'; var previousFancyInput = global.FancyInput;