Skip to content

Latest commit

 

History

History
332 lines (241 loc) · 4.27 KB

STYLEGUIDE.md

File metadata and controls

332 lines (241 loc) · 4.27 KB

Ember.js JavaScript Style Guide

Table Of Contents

Objects

  • Use literal form for object creation.
var foo = {};
  • Pad single-line objects with white-space.
var bar = { color: 'orange' };

Arrays

  • Use literal form for array creation (unless you know the exact length).
var foo = [];
  • If you know the exact length and know that array is not going to grow, use Array.
var foo = new Array(16);
  • Use push to add an item to an array.
var foo = [];
foo.push('bar');

Variables

  • Put all non-assigning declarations on one line.
var a, b;
  • Use a single var declaration for each assignment.
var a = 1;
var b = 2;
  • Declare variables at the top of their scope.
function foo() {
  var bar;

  console.log('foo bar!');

  bar = getBar();
}

Whitespace

  • Use soft tabs set to 2 spaces.
function() {
∙∙var name;
}
  • Place 1 space before the leading brace.
obj.set('foo', {
  foo: 'bar'
});

test('foo-bar', function() {
});
  • No spaces before semicolons.
var foo = {};
  • Keep parenthesis adjacent to the function name when declared or called.
function foo() {
}

foo();

Commas

  • Skip trailing commas.
var foo = [1, 2, 3];
var bar = { a: 'a' };
  • Skip leading commas.
var foo = [
  1,
  2,
  3
];

Semicolons

  • Use semicolons.

Block Statements

  • Use spaces.
// conditional
if (notFound) {
  return 0;
} else {
  return 1;
}

switch (condition) {
  case 'yes':
    // code
    break;
}

// loops
for (var key in keys) {
  // code
}

for (var i = 0, l = keys.length; i < l; i++) {
  // code
}

while (true) {
  // code
}

try {
  // code that throws an error
} catch(e) {
  // code that handles an error
}
  • Opening curly brace should be on the same line as the beginning of a statement or declaration.
function foo() {
  var obj = {
    val: 'test'
  };

  return {
    data: obj
  };
}

if (foo === 1) {
  foo();
}

for (var key in keys) {
  bar(e);
}

while (true) {
  foo();
}
  • Keep else and its accompanying braces on the same line.
if (foo === 1) {
  bar = 2;
} else {
  bar = '2';
}

if (foo === 1) {
  bar = 2;
} else if (foo === 2) {
  bar = 1;
} else {
  bar = 3;
}

Conditional Statements

  • Use === and !==.
  • Use curly braces.
if (notFound) {
  return;
}
  • Use explicit conditions.
if (arr.length > 0) {
  // code
}

if (foo !== '') {
  // code
}

Properties

  • Use dot-notation when accessing properties.
var foo = {
  bar: 'bar'
};

foo.bar;
  • Use [] when accessing properties with a variable.
var propertyName = 'bar';
var foo = {
  bar: 'bar'
};

foo[propertyName];

Functions

  • Make sure to name functions when you define them.
function fooBar() {
}

Function Arguments

arguments object must not be passed or leaked anywhere. See the reference.

  • Use a for loop with arguments (instead of slice).
function fooBar() {
  var args = new Array(arguments.length);

  for (var i = 0; i < args.length; ++i) {
    args[i] = arguments[i];
  }

  return args;
}
  • Don't re-assign the arguments.
function fooBar() {
  arguments = 3;
}

function fooBar(opt) {
  opt = 3;
}
  • Use a new variable if you need to re-assign an argument.
function fooBar(opt) {
  var options = opt;

  options = 3;
}

Comments

  • Use YUIDoc comments for documenting functions.
  • Use // for single line comments.
function foo() {
  var bar = 5;

  // multiplies `bar` by 2.
  fooBar(bar);

  console.log(bar);
}