diff --git a/README.md b/README.md index 682b6f3425..9e4700d55a 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,63 @@ -[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/airbnb/javascript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) - # Airbnb JavaScript Style Guide() { -*A mostly reasonable approach to JavaScript* - -[For the ES5-only guide click here](es5/). - -## Table of Contents - - 1. [Types](#types) - 1. [References](#references) - 1. [Objects](#objects) - 1. [Arrays](#arrays) - 1. [Destructuring](#destructuring) - 1. [Strings](#strings) - 1. [Functions](#functions) - 1. [Arrow Functions](#arrow-functions) - 1. [Constructors](#constructors) - 1. [Modules](#modules) - 1. [Iterators and Generators](#iterators-and-generators) - 1. [Properties](#properties) - 1. [Variables](#variables) - 1. [Hoisting](#hoisting) - 1. [Comparison Operators & Equality](#comparison-operators--equality) - 1. [Blocks](#blocks) - 1. [Comments](#comments) - 1. [Whitespace](#whitespace) - 1. [Commas](#commas) - 1. [Semicolons](#semicolons) - 1. [Type Casting & Coercion](#type-casting--coercion) - 1. [Naming Conventions](#naming-conventions) - 1. [Accessors](#accessors) - 1. [Events](#events) +**用更合理的方式写 JavaScript** + +[![Downloads](https://img.shields.io/npm/dm/eslint-config-airbnb.svg)](https://www.npmjs.com/package/eslint-config-airbnb) +[![Downloads](https://img.shields.io/npm/dm/eslint-config-airbnb-base.svg)](https://www.npmjs.com/package/eslint-config-airbnb-base) +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/airbnb/javascript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) + +ES5 的编码规范请查看[版本一](https://github.com/sivan/javascript-style-guide/blob/master/es5/README.md),[版本二](https://github.com/adamlu/javascript-style-guide)。 + +翻译自 [Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript) 。 + + +## 目录 + + 1. [类型](#types) + 1. [引用](#references) + 1. [对象](#objects) + 1. [数组](#arrays) + 1. [解构](#destructuring) + 1. [字符串](#strings) + 1. [函数](#functions) + 1. [箭头函数](#arrow-functions) + 1. [构造函数](#constructors) + 1. [模块](#modules) + 1. [迭代器和生成器](#iterators-and-generators) + 1. [属性](#properties) + 1. [变量](#variables) + 1. [提升](#hoisting) + 1. [比较运算符和等号](#comparison-operators--equality) + 1. [代码块](#blocks) + 1. [注释](#comments) + 1. [空白](#whitespace) + 1. [逗号](#commas) + 1. [分号](#semicolons) + 1. [类型转换](#type-casting--coercion) + 1. [命名规则](#naming-conventions) + 1. [存取器](#accessors) + 1. [事件](#events) 1. [jQuery](#jquery) - 1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility) - 1. [ECMAScript 6 Styles](#ecmascript-6-styles) - 1. [Testing](#testing) - 1. [Performance](#performance) - 1. [Resources](#resources) - 1. [In the Wild](#in-the-wild) - 1. [Translation](#translation) - 1. [The JavaScript Style Guide Guide](#the-javascript-style-guide-guide) - 1. [Chat With Us About Javascript](#chat-with-us-about-javascript) - 1. [Contributors](#contributors) - 1. [License](#license) - -## Types - - - [1.1](#1.1) **Primitives**: When you access a primitive type you work directly on its value. - - + `string` - + `number` - + `boolean` + 1. [ECMAScript 5 兼容性](#ecmascript-5-compatibility) + 1. [ECMAScript 6 编码规范](#ecmascript-6-styles) + 1. [测试](#testing) + 1. [性能](#performance) + 1. [相关资源](#resources) + 1. [使用情况](#in-the-wild) + 1. [其他翻译](#translation) + 1. [JavaScript 编码规范说明](#the-javascript-style-guide-guide) + 1. [讨论 JavaScript](#chat-with-us-about-javascript) + 1. [贡献者](#contributors) + 1. [许可协议](#license) + + +## 类型 + + - [1.1](#1.1) **基本类型**: 直接存取基本类型。 + + + `字符串` + + `数值` + + `布尔类型` + `null` + `undefined` @@ -63,11 +69,12 @@ console.log(foo, bar); // => 1, 9 ``` - - [1.2](#1.2) **Complex**: When you access a complex type you work on a reference to its value. - + `object` - + `array` - + `function` + - [1.2](#1.2) **复杂类型**: 通过引用的方式存取复杂类型。 + + + `对象` + + `数组` + + `函数` ```javascript const foo = [1, 2]; @@ -78,13 +85,14 @@ console.log(foo[0], bar[0]); // => 9, 9 ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## References + +## 引用 - - [2.1](#2.1) Use `const` for all of your references; avoid using `var`. + - [2.1](#2.1) 对所有的引用使用 `const` ;不要使用 `var`。 - > Why? This ensures that you can't reassign your references (mutation), which can lead to bugs and difficult to comprehend code. + > 为什么?这能确保你无法对引用重新赋值,也不会导致出现 bug 或难以理解。 ```javascript // bad @@ -96,9 +104,9 @@ const b = 2; ``` - - [2.2](#2.2) If you must mutate references, use `let` instead of `var`. + - [2.2](#2.2) 如果你一定需要可变动的引用,使用 `let` 代替 `var`。 - > Why? `let` is block-scoped rather than function-scoped like `var`. + > 为什么?因为 `let` 是块级作用域,而 `var` 是函数作用域。 ```javascript // bad @@ -114,10 +122,10 @@ } ``` - - [2.3](#2.3) Note that both `let` and `const` are block-scoped. + - [2.3](#2.3) 注意 `let` 和 `const` 都是块级作用域。 ```javascript - // const and let only exist in the blocks they are defined in. + // const 和 let 只存在于它们被定义的区块内。 { let a = 1; const b = 1; @@ -126,11 +134,12 @@ console.log(b); // ReferenceError ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## Objects + +## 对象 - - [3.1](#3.1) Use the literal syntax for object creation. + - [3.1](#3.1) 使用字面值创建对象。 ```javascript // bad @@ -140,7 +149,7 @@ const item = {}; ``` - - [3.2](#3.2) If your code will be executed in browsers in script context, don't use [reserved words](http://es5.github.io/#x7.6.1) as keys. It won't work in IE8. [More info](https://github.com/airbnb/javascript/issues/61). It’s OK to use them in ES6 modules and server-side code. + - [3.2](#3.2) 如果你的代码在浏览器环境下执行,别使用 [保留字](http://es5.github.io/#x7.6.1) 作为键值。这样的话在 IE8 不会运行。 [更多信息](https://github.com/airbnb/javascript/issues/61)。 但在 ES6 模块和服务器端中使用没有问题。 ```javascript // bad @@ -156,7 +165,7 @@ }; ``` - - [3.3](#3.3) Use readable synonyms in place of reserved words. + - [3.3](#3.3) 使用同义词替换需要使用的保留字。 ```javascript // bad @@ -176,12 +185,11 @@ ``` - - [3.4](#3.4) Use computed property names when creating objects with dynamic property names. + - [3.4](#3.4) 创建有动态属性名的对象时,使用可被计算的属性名称。 - > Why? They allow you to define all the properties of an object in one place. + > 为什么?因为这样可以让你在一个地方定义所有的对象属性。 ```javascript - function getKey(k) { return `a key named ${k}`; } @@ -202,7 +210,7 @@ ``` - - [3.5](#3.5) Use object method shorthand. + - [3.5](#3.5) 使用对象方法的简写。 ```javascript // bad @@ -225,9 +233,9 @@ ``` - - [3.6](#3.6) Use property value shorthand. + - [3.6](#3.6) 使用对象属性值的简写。 - > Why? It is shorter to write and descriptive. + > 为什么?因为这样更短更有描述性。 ```javascript const lukeSkywalker = 'Luke Skywalker'; @@ -243,9 +251,9 @@ }; ``` - - [3.7](#3.7) Group your shorthand properties at the beginning of your object declaration. + - [3.7](#3.7) 在对象属性声明前把简写的属性分组。 - > Why? It's easier to tell which properties are using the shorthand. + > 为什么?因为这样能清楚地看出哪些属性使用了简写。 ```javascript const anakinSkywalker = 'Anakin Skywalker'; @@ -272,11 +280,12 @@ }; ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## Arrays + +## 数组 - - [4.1](#4.1) Use the literal syntax for array creation. + - [4.1](#4.1) 使用字面值创建数组。 ```javascript // bad @@ -286,7 +295,7 @@ const items = []; ``` - - [4.2](#4.2) Use Array#push instead of direct assignment to add items to an array. + - [4.2](#4.2) 向数组添加元素时使用 Arrary#push 替代直接赋值。 ```javascript const someStack = []; @@ -300,7 +309,7 @@ ``` - - [4.3](#4.3) Use array spreads `...` to copy arrays. + - [4.3](#4.3) 使用拓展运算符 `...` 复制数组。 ```javascript // bad @@ -315,20 +324,21 @@ // good const itemsCopy = [...items]; ``` - - [4.4](#4.4) To convert an array-like object to an array, use Array#from. + - [4.4](#4.4) 使用 Array#from 把一个类数组对象转换成数组。 ```javascript const foo = document.querySelectorAll('.foo'); const nodes = Array.from(foo); ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## Destructuring + +## 解构 - - [5.1](#5.1) Use object destructuring when accessing and using multiple properties of an object. + - [5.1](#5.1) 使用解构存取和使用多属性对象。 - > Why? Destructuring saves you from creating temporary references for those properties. + > 为什么?因为解构能减少临时引用属性。 ```javascript // bad @@ -351,7 +361,7 @@ } ``` - - [5.2](#5.2) Use array destructuring. + - [5.2](#5.2) 对数组使用解构赋值。 ```javascript const arr = [1, 2, 3, 4]; @@ -364,9 +374,8 @@ const [first, second] = arr; ``` - - [5.3](#5.3) Use object destructuring for multiple return values, not array destructuring. - - > Why? You can add new properties over time or change the order of things without breaking call sites. + - [5.3](#5.3) 需要回传多个值时,使用对象解构,而不是数组解构。 + > 为什么?增加属性或者改变排序不会改变调用时的位置。 ```javascript // bad @@ -375,7 +384,7 @@ return [left, right, top, bottom]; } - // the caller needs to think about the order of return data + // 调用时需要考虑回调数据的顺序。 const [left, __, top] = processInput(input); // good @@ -384,16 +393,17 @@ return { left, right, top, bottom }; } - // the caller selects only the data they need + // 调用时只选择需要的数据 const { left, right } = processInput(input); ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + ## Strings - - [6.1](#6.1) Use single quotes `''` for strings. + - [6.1](#6.1) 字符串使用单引号 `''` 。 ```javascript // bad @@ -403,8 +413,8 @@ const name = 'Capt. Janeway'; ``` - - [6.2](#6.2) Strings longer than 80 characters should be written across multiple lines using string concatenation. - - [6.3](#6.3) Note: If overused, long strings with concatenation could impact performance. [jsPerf](http://jsperf.com/ya-string-concat) & [Discussion](https://github.com/airbnb/javascript/issues/40). + - [6.2](#6.2) 字符串超过 80 个字节应该使用字符串连接号换行。 + - [6.3](#6.3) 注:过度使用字串连接符号可能会对性能造成影响。[jsPerf](http://jsperf.com/ya-string-concat) 和 [讨论](https://github.com/airbnb/javascript/issues/40). ```javascript // bad @@ -423,9 +433,9 @@ ``` - - [6.4](#6.4) When programmatically building up strings, use template strings instead of concatenation. + - [6.4](#6.4) 程序化生成字符串时,使用模板字符串代替字符串连接。 - > Why? Template strings give you a readable, concise syntax with proper newlines and string interpolation features. + > 为什么?模板字符串更为简洁,更具可读性。 ```javascript // bad @@ -444,14 +454,14 @@ } ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + +## 函数 -## Functions + - [7.1](#7.1) 使用函数声明代替函数表达式。 - - [7.1](#7.1) Use function declarations instead of function expressions. - - > Why? Function declarations are named, so they're easier to identify in call stacks. Also, the whole body of a function declaration is hoisted, whereas only the reference of a function expression is hoisted. This rule makes it possible to always use [Arrow Functions](#arrow-functions) in place of function expressions. + > 为什么?因为函数声明是可命名的,所以他们在调用栈中更容易被识别。此外,函数声明会把整个函数提升(hoisted),而函数表达式只会把函数的引用变量名提升。这条规则使得[箭头函数](#arrow-functions)可以取代函数表达式。 ```javascript // bad @@ -463,17 +473,17 @@ } ``` - - [7.2](#7.2) Function expressions: + - [7.2](#7.2) 函数表达式: ```javascript - // immediately-invoked function expression (IIFE) + // 立即调用的函数表达式 (IIFE) (() => { console.log('Welcome to the Internet. Please follow me.'); })(); ``` - - [7.3](#7.3) Never declare a function in a non-function block (if, while, etc). Assign the function to a variable instead. Browsers will allow you to do it, but they all interpret it differently, which is bad news bears. - - [7.4](#7.4) **Note:** ECMA-262 defines a `block` as a list of statements. A function declaration is not a statement. [Read ECMA-262's note on this issue](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf#page=97). + - [7.3](#7.3) 永远不要在一个非函数代码块(`if`、`while` 等)中声明一个函数,把那个函数赋给一个变量。浏览器允许你这么做,但它们的解析表现不一致。 + - [7.4](#7.4) **注意:** ECMA-262 把 `block` 定义为一组语句。函数声明不是语句。[阅读 ECMA-262 关于这个问题的说明](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf#page=97)。 ```javascript // bad @@ -492,7 +502,7 @@ } ``` - - [7.5](#7.5) Never name a parameter `arguments`. This will take precedence over the `arguments` object that is given to every function scope. + - [7.5](#7.5) 永远不要把参数命名为 `arguments`。这将取代原来函数作用域内的 `arguments` 对象。 ```javascript // bad @@ -507,9 +517,9 @@ ``` - - [7.6](#7.6) Never use `arguments`, opt to use rest syntax `...` instead. + - [7.6](#7.6) 不要使用 `arguments`。可以选择 rest 语法 `...` 替代。 - > Why? `...` is explicit about which arguments you want pulled. Plus rest arguments are a real Array and not Array-like like `arguments`. + > 为什么?使用 `...` 能明确你要传入的参数。另外 rest 参数是一个真正的数组,而 `arguments` 是一个类数组。 ```javascript // bad @@ -525,14 +535,15 @@ ``` - - [7.7](#7.7) Use default parameter syntax rather than mutating function arguments. + - [7.7](#7.7) 直接给函数的参数指定默认值,不要使用一个变化的函数参数。 ```javascript // really bad function handleThings(opts) { - // No! We shouldn't mutate function arguments. - // Double bad: if opts is falsy it'll be set to an object which may - // be what you want but it can introduce subtle bugs. + // 不!我们不应该改变函数参数。 + // 更加糟糕: 如果参数 opts 是 false 的话,它就会被设定为一个对象。 + // 但这样的写法会造成一些 Bugs。 + //(译注:例如当 opts 被赋值为空字符串,opts 仍然会被下一行代码设定为一个空对象。) opts = opts || {}; // ... } @@ -551,9 +562,9 @@ } ``` - - [7.8](#7.8) Avoid side effects with default parameters + - [7.8](#7.8) 直接给函数参数赋值时需要避免副作用。 - > Why? They are confusing to reason about. + > 为什么?因为这样的写法让人感到很困惑。 ```javascript var b = 1; @@ -568,33 +579,36 @@ ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## Arrow Functions + +## 箭头函数 - - [8.1](#8.1) When you must use function expressions (as when passing an anonymous function), use arrow function notation. + - [8.1](#8.1) 当你必须使用函数表达式(或传递一个匿名函数)时,使用箭头函数符号。 - > Why? It creates a version of the function that executes in the context of `this`, which is usually what you want, and is a more concise syntax. + > 为什么?因为箭头函数创造了新的一个 `this` 执行环境(译注:参考 [Arrow functions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) 和 [ES6 arrow functions, syntax and lexical scoping](http://toddmotto.com/es6-arrow-functions-syntaxes-and-lexical-scoping/)),通常情况下都能满足你的需求,而且这样的写法更为简洁。 - > Why not? If you have a fairly complicated function, you might move that logic out into its own function declaration. + > 为什么不?如果你有一个相当复杂的函数,你或许可以把逻辑部分转移到一个函数声明上。 ```javascript // bad [1, 2, 3].map(function (x) { - return x * x; + const y = x + 1; + return x * y; }); // good [1, 2, 3].map((x) => { - return x * x; + const y = x + 1; + return x * y; }); ``` - - [8.2](#8.2) If the function body fits on one line and there is only a single argument, feel free to omit the braces and parentheses, and use the implicit return. Otherwise, add the parentheses, braces, and use a `return` statement. + - [8.2](#8.2) 如果一个函数适合用一行写出并且只有一个参数,那就把花括号、圆括号和 `return` 都省略掉。如果不是,那就不要省略。 - > Why? Syntactic sugar. It reads well when multiple functions are chained together. + > 为什么?语法糖。在链式调用中可读性很高。 - > Why not? If you plan on returning an object. + > 为什么不?当你打算回传一个对象的时候。 ```javascript // good @@ -606,14 +620,14 @@ }, 0); ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## Constructors + +## 构造器 - - [9.1](#9.1) Always use `class`. Avoid manipulating `prototype` directly. + - [9.1](#9.1) 总是使用 `class`。避免直接操作 `prototype` 。 - > Why? `class` syntax is more concise and easier to reason about. + > 为什么? 因为 `class` 语法更为简洁更易读。 ```javascript // bad @@ -640,9 +654,9 @@ } ``` - - [9.2](#9.2) Use `extends` for inheritance. + - [9.2](#9.2) 使用 `extends` 继承。 - > Why? It is a built-in way to inherit prototype functionality without breaking `instanceof`. + > 为什么?因为 `extends` 是一个内建的原型继承方法并且不会破坏 `instanceof`。 ```javascript // bad @@ -663,7 +677,7 @@ } ``` - - [9.3](#9.3) Methods can return `this` to help with method chaining. + - [9.3](#9.3) 方法可以返回 `this` 来帮助链式调用。 ```javascript // bad @@ -700,7 +714,7 @@ ``` - - [9.4](#9.4) It's okay to write a custom toString() method, just make sure it works successfully and causes no side effects. + - [9.4](#9.4) 可以写一个自定义的 `toString()` 方法,但要确保它能正常运行并且不会引起副作用。 ```javascript class Jedi { @@ -718,14 +732,14 @@ } ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## Modules + +## 模块 - - [10.1](#10.1) Always use modules (`import`/`export`) over a non-standard module system. You can always transpile to your preferred module system. + - [10.1](#10.1) 总是使用模组 (`import`/`export`) 而不是其他非标准模块系统。你可以编译为你喜欢的模块系统。 - > Why? Modules are the future, let's start using the future now. + > 为什么?模块就是未来,让我们开始迈向未来吧。 ```javascript // bad @@ -741,9 +755,9 @@ export default es6; ``` - - [10.2](#10.2) Do not use wildcard imports. + - [10.2](#10.2) 不要使用通配符 import。 - > Why? This makes sure you have a single default export. + > 为什么?这样能确保你只有一个默认 export。 ```javascript // bad @@ -753,9 +767,9 @@ import AirbnbStyleGuide from './AirbnbStyleGuide'; ``` - - [10.3](#10.3) And do not export directly from an import. + - [10.3](#10.3) 不要从 import 中直接 export。 - > Why? Although the one-liner is concise, having one clear way to import and one clear way to export makes things consistent. + > 为什么?虽然一行代码简洁明了,但让 import 和 export 各司其职让事情能保持一致。 ```javascript // bad @@ -768,13 +782,14 @@ export default es6; ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + ## Iterators and Generators - - [11.1](#11.1) Don't use iterators. Prefer JavaScript's higher-order functions like `map()` and `reduce()` instead of loops like `for-of`. + - [11.1](#11.1) 不要使用 iterators。使用高阶函数例如 `map()` 和 `reduce()` 替代 `for-of`。 - > Why? This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side-effects. + > 为什么?这加强了我们不变的规则。处理纯函数的回调值更易读,这比它带来的副作用更重要。 ```javascript const numbers = [1, 2, 3, 4, 5]; @@ -797,16 +812,16 @@ sum === 15; ``` - - [11.2](#11.2) Don't use generators for now. + - [11.2](#11.2) 现在还不要使用 generators。 - > Why? They don't transpile well to ES5. + > 为什么?因为它们现在还没法很好地编译到 ES5。 (译者注:目前(2016/03) Chrome 和 Node.js 的稳定版本都已支持 generators) -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + +## 属性 -## Properties - - - [12.1](#12.1) Use dot notation when accessing properties. + - [12.1](#12.1) 使用 `.` 来访问对象的属性。 ```javascript const luke = { @@ -821,7 +836,7 @@ const isJedi = luke.jedi; ``` - - [12.2](#12.2) Use subscript notation `[]` when accessing properties with a variable. + - [12.2](#12.2) 当通过变量访问属性时使用中括号 `[]`。 ```javascript const luke = { @@ -836,12 +851,12 @@ const isJedi = getProp('jedi'); ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## Variables + +## 变量 - - [13.1](#13.1) Always use `const` to declare variables. Not doing so will result in global variables. We want to avoid polluting the global namespace. Captain Planet warned us of that. + - [13.1](#13.1) 一直使用 `const` 来声明变量,如果不这样做就会产生全局变量。我们需要避免全局命名空间的污染。[地球队长](http://www.wikiwand.com/en/Captain_Planet)已经警告过我们了。(译注:全局,global 亦有全球的意思。地球队长的责任是保卫地球环境,所以他警告我们不要造成「全球」污染。) ```javascript // bad @@ -851,9 +866,9 @@ const superPower = new SuperPower(); ``` - - [13.2](#13.2) Use one `const` declaration per variable. + - [13.2](#13.2) 使用 `const` 声明每一个变量。 - > Why? It's easier to add new variable declarations this way, and you never have to worry about swapping out a `;` for a `,` or introducing punctuation-only diffs. + > 为什么?增加新变量将变的更加容易,而且你永远不用再担心调换错 `;` 跟 `,`。 ```javascript // bad @@ -873,9 +888,9 @@ const dragonball = 'z'; ``` - - [13.3](#13.3) Group all your `const`s and then group all your `let`s. + - [13.3](#13.3) 将所有的 `const` 和 `let` 分组 - > Why? This is helpful when later on you might need to assign a variable depending on one of the previous assigned variables. + > 为什么?当你需要把已赋值变量赋值给未赋值变量时非常有用。 ```javascript // bad @@ -898,9 +913,9 @@ let length; ``` - - [13.4](#13.4) Assign variables where you need them, but place them in a reasonable place. + - [13.4](#13.4) 在你需要的地方给变量赋值,但请把它们放在一个合理的位置。 - > Why? `let` and `const` are block scoped and not function scoped. + > 为什么?`let` 和 `const` 是块级作用域而不是函数作用域。 ```javascript // good @@ -945,39 +960,37 @@ } ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** + ## Hoisting - - [14.1](#14.1) `var` declarations get hoisted to the top of their scope, their assignment does not. `const` and `let` declarations are blessed with a new concept called [Temporal Dead Zones (TDZ)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let). It's important to know why [typeof is no longer safe](http://es-discourse.com/t/why-typeof-is-no-longer-safe/15). + - [14.1](#14.1) `var` 声明会被提升至该作用域的顶部,但它们赋值不会提升。`let` 和 `const` 被赋予了一种称为「[暂时性死区(Temporal Dead Zones, TDZ)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let)」的概念。这对于了解为什么 [type of 不再安全](http://es-discourse.com/t/why-typeof-is-no-longer-safe/15)相当重要。 ```javascript - // we know this wouldn't work (assuming there - // is no notDefined global variable) + // 我们知道这样运行不了 + // (假设 notDefined 不是全局变量) function example() { console.log(notDefined); // => throws a ReferenceError } - // creating a variable declaration after you - // reference the variable will work due to - // variable hoisting. Note: the assignment - // value of `true` is not hoisted. + // 由于变量提升的原因, + // 在引用变量后再声明变量是可以运行的。 + // 注:变量的赋值 `true` 不会被提升。 function example() { console.log(declaredButNotAssigned); // => undefined var declaredButNotAssigned = true; } - // The interpreter is hoisting the variable - // declaration to the top of the scope, - // which means our example could be rewritten as: + // 编译器会把函数声明提升到作用域的顶层, + // 这意味着我们的例子可以改写成这样: function example() { let declaredButNotAssigned; console.log(declaredButNotAssigned); // => undefined declaredButNotAssigned = true; } - // using const and let + // 使用 const 和 let function example() { console.log(declaredButNotAssigned); // => throws a ReferenceError console.log(typeof declaredButNotAssigned); // => throws a ReferenceError @@ -985,7 +998,7 @@ } ``` - - [14.2](#14.2) Anonymous function expressions hoist their variable name, but not the function assignment. + - [14.2](#14.2) 匿名函数表达式的变量名会被提升,但函数内容并不会。 ```javascript function example() { @@ -999,7 +1012,7 @@ } ``` - - [14.3](#14.3) Named function expressions hoist the variable name, not the function name or the function body. + - [14.3](#14.3) 命名的函数表达式的变量名会被提升,但函数名和函数函数内容并不会。 ```javascript function example() { @@ -1027,7 +1040,7 @@ } ``` - - [14.4](#14.4) Function declarations hoist their name and the function body. + - [14.4](#14.4) 函数声明的名称和函数体都会被提升。 ```javascript function example() { @@ -1039,22 +1052,22 @@ } ``` - - For more information refer to [JavaScript Scoping & Hoisting](http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting) by [Ben Cherry](http://www.adequatelygood.com/). - -**[⬆ back to top](#table-of-contents)** + - 想了解更多信息,参考 [Ben Cherry](http://www.adequatelygood.com/) 的 [JavaScript Scoping & Hoisting](http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting)。 +**[⬆ 返回目录](#table-of-contents)** -## Comparison Operators & Equality + +## 比较运算符和等号 - - [15.1](#15.1) Use `===` and `!==` over `==` and `!=`. - - [15.2](#15.2) Conditional statements such as the `if` statement evaluate their expression using coercion with the `ToBoolean` abstract method and always follow these simple rules: + - [15.1](#15.1) 优先使用 `===` 和 `!==` 而不是 `==` 和 `!=`. + - [15.2](#15.2) 条件表达式例如 `if` 语句通过抽象方法 `ToBoolean` 强制计算它们的表达式并且总是遵守下面的规则: - + **Objects** evaluate to **true** - + **Undefined** evaluates to **false** - + **Null** evaluates to **false** - + **Booleans** evaluate to **the value of the boolean** - + **Numbers** evaluate to **false** if **+0, -0, or NaN**, otherwise **true** - + **Strings** evaluate to **false** if an empty string `''`, otherwise **true** + + **对象** 被计算为 **true** + + **Undefined** 被计算为 **false** + + **Null** 被计算为 **false** + + **布尔值** 被计算为 **布尔的值** + + **数字** 如果是 **+0、-0、或 NaN** 被计算为 **false**, 否则为 **true** + + **字符串** 如果是空字符串 `''` 被计算为 **false**,否则为 **true** ```javascript if ([0]) { @@ -1063,7 +1076,7 @@ } ``` - - [15.3](#15.3) Use shortcuts. + - [15.3](#15.3) 使用简写。 ```javascript // bad @@ -1087,14 +1100,14 @@ } ``` - - [15.4](#15.4) For more information see [Truth Equality and JavaScript](http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/#more-2108) by Angus Croll. + - [15.4](#15.4) 想了解更多信息,参考 Angus Croll 的 [Truth Equality and JavaScript](http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/#more-2108)。 -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + +## 代码块 -## Blocks - - - [16.1](#16.1) Use braces with all multi-line blocks. + - [16.1](#16.1) 使用大括号包裹所有的多行代码块。 ```javascript // bad @@ -1118,8 +1131,7 @@ } ``` - - [16.2](#16.2) If you're using multi-line blocks with `if` and `else`, put `else` on the same line as your - `if` block's closing brace. + - [16.2](#16.2) 如果通过 `if` 和 `else` 使用多行代码块,把 `else` 放在 `if` 代码块关闭括号的同一行。 ```javascript // bad @@ -1141,12 +1153,12 @@ ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## Comments + +## 注释 - - [17.1](#17.1) Use `/** ... */` for multi-line comments. Include a description, specify types and values for all parameters and return values. + - [17.1](#17.1) 使用 `/** ... */` 作为多行注释。包含描述、指定所有参数和返回值的类型和值。 ```javascript // bad @@ -1178,7 +1190,7 @@ } ``` - - [17.2](#17.2) Use `//` for single line comments. Place single line comments on a newline above the subject of the comment. Put an empty line before the comment. + - [17.2](#17.2) 使用 `//` 作为单行注释。在评论对象上面另起一行使用单行注释。在注释前插入空行。 ```javascript // bad @@ -1208,9 +1220,9 @@ } ``` - - [17.3](#17.3) Prefixing your comments with `FIXME` or `TODO` helps other developers quickly understand if you're pointing out a problem that needs to be revisited, or if you're suggesting a solution to the problem that needs to be implemented. These are different than regular comments because they are actionable. The actions are `FIXME -- need to figure this out` or `TODO -- need to implement`. + - [17.3](#17.3) 给注释增加 `FIXME` 或 `TODO` 的前缀可以帮助其他开发者快速了解这是一个需要复查的问题,或是给需要实现的功能提供一个解决方式。这将有别于常见的注释,因为它们是可操作的。使用 `FIXME -- need to figure this out` 或者 `TODO -- need to implement`。 - - [17.4](#17.4) Use `// FIXME:` to annotate problems. + - [17.4](#17.4) 使用 `// FIXME`: 标注问题。 ```javascript class Calculator { @@ -1221,7 +1233,7 @@ } ``` - - [17.5](#17.5) Use `// TODO:` to annotate solutions to problems. + - [17.5](#17.5) 使用 `// TODO`: 标注问题的解决方式。 ```javascript class Calculator { @@ -1232,12 +1244,12 @@ } ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## Whitespace + +## 空白 - - [18.1](#18.1) Use soft tabs set to 2 spaces. + - [18.1](#18.1) 使用 2 个空格作为缩进。 ```javascript // bad @@ -1256,7 +1268,7 @@ } ``` - - [18.2](#18.2) Place 1 space before the leading brace. + - [18.2](#18.2) 在花括号前放一个空格。 ```javascript // bad @@ -1282,7 +1294,7 @@ }); ``` - - [18.3](#18.3) Place 1 space before the opening parenthesis in control statements (`if`, `while` etc.). Place no space before the argument list in function calls and declarations. + - [18.3](#18.3) 在控制语句(`if`、`while` 等)的小括号前放一个空格。在函数调用及声明中,不在函数的参数列表前加空格。 ```javascript // bad @@ -1306,7 +1318,7 @@ } ``` - - [18.4](#18.4) Set off operators with spaces. + - [18.4](#18.4) 使用空格把运算符隔开。 ```javascript // bad @@ -1316,7 +1328,7 @@ const x = y + 5; ``` - - [18.5](#18.5) End files with a single newline character. + - [18.5](#18.5) 在文件末尾插入一个空行。 ```javascript // bad @@ -1340,8 +1352,7 @@ })(this);↵ ``` - - [18.5](#18.5) Use indentation when making long method chains. Use a leading dot, which - emphasizes that the line is a method call, not a new statement. + - [18.5](#18.5) 在使用长方法链时进行缩进。使用前面的点 `.` 强调这是方法调用而不是新语句。 ```javascript // bad @@ -1380,7 +1391,7 @@ .call(tron.led); ``` - - [18.6](#18.6) Leave a blank line after blocks and before the next statement. + - [18.6](#18.6) 在块末和新语句前插入空行。 ```javascript // bad @@ -1418,11 +1429,12 @@ ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## Commas + +## 逗号 - - [19.1](#19.1) Leading commas: **Nope.** + - [19.1](#19.1) 行首逗号:**不需要**。 ```javascript // bad @@ -1456,9 +1468,9 @@ }; ``` - - [19.2](#19.2) Additional trailing comma: **Yup.** + - [19.2](#19.2) 增加结尾的逗号: **需要**。 - > Why? This leads to cleaner git diffs. Also, transpilers like Babel will remove the additional trailing comma in the transpiled code which means you don't have to worry about the [trailing comma problem](es5/README.md#commas) in legacy browsers. + > 为什么? 这会让 git diffs 更干净。另外,像 babel 这样的转译器会移除结尾多余的逗号,也就是说你不必担心老旧浏览器的[尾逗号问题](es5/README.md#commas)。 ```javascript // bad - git diff without trailing comma @@ -1499,12 +1511,12 @@ ]; ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + +## 分号 -## Semicolons - - - [20.1](#20.1) **Yup.** + - [20.1](#20.1) **使用分号** ```javascript // bad @@ -1519,7 +1531,7 @@ return name; })(); - // good (guards against the function becoming an argument when two files with IIFEs are concatenated) + // good (防止函数在两个 IIFE 合并时被当成一个参数) ;(() => { const name = 'Skywalker'; return name; @@ -1528,13 +1540,13 @@ [Read more](http://stackoverflow.com/a/7365214/1712802). -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## Type Casting & Coercion + +## 类型转换 - - [21.1](#21.1) Perform type coercion at the beginning of the statement. - - [21.2](#21.2) Strings: + - [21.1](#21.1) 在语句开始时执行类型转换。 + - [21.2](#21.2) 字符串: ```javascript // => this.reviewScore = 9; @@ -1546,7 +1558,7 @@ const totalScore = String(this.reviewScore); ``` - - [21.3](#21.3) Use `parseInt` for Numbers and always with a radix for type casting. + - [21.3](#21.3) 对数字使用 `parseInt` 转换,并带上类型转换的基数。 ```javascript const inputValue = '4'; @@ -1570,19 +1582,18 @@ const val = parseInt(inputValue, 10); ``` - - [21.4](#21.4) If for whatever reason you are doing something wild and `parseInt` is your bottleneck and need to use Bitshift for [performance reasons](http://jsperf.com/coercion-vs-casting/3), leave a comment explaining why and what you're doing. + - [21.4](#21.4) 如果因为某些原因 parseInt 成为你所做的事的瓶颈而需要使用位操作解决[性能问题](http://jsperf.com/coercion-vs-casting/3)时,留个注释说清楚原因和你的目的。 ```javascript // good /** - * parseInt was the reason my code was slow. - * Bitshifting the String to coerce it to a - * Number made it a lot faster. + * 使用 parseInt 导致我的程序变慢, + * 改成使用位操作转换数字快多了。 */ const val = inputValue >> 0; ``` - - [21.5](#21.5) **Note:** Be careful when using bitshift operations. Numbers are represented as [64-bit values](http://es5.github.io/#x4.3.19), but Bitshift operations always return a 32-bit integer ([source](http://es5.github.io/#x11.7)). Bitshift can lead to unexpected behavior for integer values larger than 32 bits. [Discussion](https://github.com/airbnb/javascript/issues/109). Largest signed 32-bit Int is 2,147,483,647: + - [21.5](#21.5) **注:** 小心使用位操作运算符。数字会被当成 [64 位值](http://es5.github.io/#x4.3.19),但是位操作运算符总是返回 32 位的整数([参考](http://es5.github.io/#x11.7))。位操作处理大于 32 位的整数值时还会导致意料之外的行为。[关于这个问题的讨论](https://github.com/airbnb/javascript/issues/109)。最大的 32 位整数是 2,147,483,647: ```javascript 2147483647 >> 0 //=> 2147483647 @@ -1590,7 +1601,7 @@ 2147483649 >> 0 //=> -2147483647 ``` - - [21.6](#21.6) Booleans: + - [21.6](#21.6) 布尔: ```javascript const age = 0; @@ -1605,12 +1616,12 @@ const hasAge = !!age; ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + +## 命名规则 -## Naming Conventions - - - [22.1](#22.1) Avoid single letter names. Be descriptive with your naming. + - [22.1](#22.1) 避免单字母命名。命名应具备描述性。 ```javascript // bad @@ -1624,7 +1635,7 @@ } ``` - - [22.2](#22.2) Use camelCase when naming objects, functions, and instances. + - [22.2](#22.2) 使用驼峰式命名对象、函数和实例。 ```javascript // bad @@ -1637,7 +1648,7 @@ function thisIsMyFunction() {} ``` - - [22.3](#22.3) Use PascalCase when naming constructors or classes. + - [22.3](#22.3) 使用帕斯卡式命名构造函数或类。 ```javascript // bad @@ -1661,18 +1672,19 @@ }); ``` - - [22.4](#22.4) Use a leading underscore `_` when naming private properties. + - [22.4](#22.4) 不要使用下划线 `_` 结尾或开头来命名属性和方法。 ```javascript // bad this.__firstName__ = 'Panda'; this.firstName_ = 'Panda'; + this._firstName = 'Panda'; // good - this._firstName = 'Panda'; + this.firstName = 'Panda'; ``` - - [22.5](#22.5) Don't save references to `this`. Use arrow functions or Function#bind. + - [22.5](#22.5) 别保存 `this` 的引用。使用箭头函数或 Function#bind。 ```javascript // bad @@ -1699,7 +1711,8 @@ } ``` - - [22.6](#22.6) If your file exports a single class, your filename should be exactly the name of the class. + - [22.6](#22.6) 如果你的文件只输出一个类,那你的文件名必须和类名完全保持一致。 + ```javascript // file contents class CheckBox { @@ -1718,7 +1731,7 @@ import CheckBox from './CheckBox'; ``` - - [22.7](#22.7) Use camelCase when you export-default a function. Your filename should be identical to your function's name. + - [22.7](#22.7) 当你导出默认的函数时使用驼峰式命名。你的文件名必须和函数名完全保持一致。 ```javascript function makeStyleGuide() { @@ -1727,7 +1740,7 @@ export default makeStyleGuide; ``` - - [22.8](#22.8) Use PascalCase when you export a singleton / function library / bare object. + - [22.8](#22.8) 当你导出单例、函数库、空对象时使用帕斯卡式命名。 ```javascript const AirbnbStyleGuide = { @@ -1739,13 +1752,13 @@ ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## Accessors + +## 存取器 - - [23.1](#23.1) Accessor functions for properties are not required. - - [23.2](#23.2) If you do make accessor functions use getVal() and setVal('hello'). + - [23.1](#23.1) 属性的存取函数不是必须的。 + - [23.2](#23.2) 如果你需要存取函数时使用 `getVal()` 和 `setVal('hello')`。 ```javascript // bad @@ -1761,7 +1774,7 @@ dragon.setAge(25); ``` - - [23.3](#23.3) If the property is a `boolean`, use `isVal()` or `hasVal()`. + - [23.3](#23.3) 如果属性是布尔值,使用 `isVal()` 或 `hasVal()`。 ```javascript // bad @@ -1775,7 +1788,7 @@ } ``` - - [23.4](#23.4) It's okay to create get() and set() functions, but be consistent. + - [23.4](#23.4) 创建 `get()` 和 `set()` 函数是可以的,但要保持一致。 ```javascript class Jedi { @@ -1794,12 +1807,12 @@ } ``` -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + +## 事件 -## Events - - - [24.1](#24.1) When attaching data payloads to events (whether DOM events or something more proprietary like Backbone events), pass a hash instead of a raw value. This allows a subsequent contributor to add more data to the event payload without finding and updating every handler for the event. For example, instead of: + - [24.1](#24.1) 当给事件附加数据时(无论是 DOM 事件还是私有事件),传入一个哈希而不是原始值。这样可以让后面的贡献者增加更多数据到事件数据而无需找出并更新事件的每一个处理器。例如,不好的写法: ```javascript // bad @@ -1812,7 +1825,7 @@ }); ``` - prefer: + 更好的写法: ```javascript // good @@ -1825,12 +1838,12 @@ }); ``` - **[⬆ back to top](#table-of-contents)** + **[⬆ 返回目录](#table-of-contents)** ## jQuery - - [25.1](#25.1) Prefix jQuery object variables with a `$`. + - [25.1](#25.1) 使用 `$` 作为存储 jQuery 对象的变量名前缀。 ```javascript // bad @@ -1840,7 +1853,7 @@ const $sidebar = $('.sidebar'); ``` - - [25.2](#25.2) Cache jQuery lookups. + - [25.2](#25.2) 缓存 jQuery 查询。 ```javascript // bad @@ -1867,8 +1880,8 @@ } ``` - - [25.3](#25.3) For DOM queries use Cascading `$('.sidebar ul')` or parent > child `$('.sidebar > ul')`. [jsPerf](http://jsperf.com/jquery-find-vs-context-sel/16) - - [25.4](#25.4) Use `find` with scoped jQuery object queries. + - [25.3](#25.3) 对 DOM 查询使用层叠 `$('.sidebar ul')` 或 父元素 > 子元素 `$('.sidebar > ul')`。 [jsPerf](http://jsperf.com/jquery-find-vs-context-sel/16) + - [25.4](#25.4) 对有作用域的 jQuery 对象查询使用 `find`。 ```javascript // bad @@ -1887,36 +1900,38 @@ $sidebar.find('ul').hide(); ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## ECMAScript 5 Compatibility + +## ECMAScript 5 兼容性 - - [26.1](#26.1) Refer to [Kangax](https://twitter.com/kangax/)'s ES5 [compatibility table](http://kangax.github.com/es5-compat-table/). + - [26.1](#26.1) 参考 [Kangax](https://twitter.com/kangax/) 的 ES5 [兼容性](http://kangax.github.com/es5-compat-table/)。 -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## ECMAScript 6 Styles + +## ECMAScript 6 规范 - - [27.1](#27.1) This is a collection of links to the various es6 features. + - [27.1](#27.1) 以下是链接到 ES6 各个特性的列表。 -1. [Arrow Functions](#arrow-functions) -1. [Classes](#constructors) -1. [Object Shorthand](#es6-object-shorthand) -1. [Object Concise](#es6-object-concise) -1. [Object Computed Properties](#es6-computed-properties) -1. [Template Strings](#es6-template-literals) -1. [Destructuring](#destructuring) -1. [Default Parameters](#es6-default-parameters) +1. [箭头函数](#arrow-functions) +1. [类](#constructors) +1. [对象方法简写](#es6-object-shorthand) +1. [对象属性简写](#es6-object-concise) +1. [对象中的可计算属性](#es6-computed-properties) +1. [模板字符串](#es6-template-literals) +1. [解构](#destructuring) +1. [默认参数](#es6-default-parameters) 1. [Rest](#es6-rest) -1. [Array Spreads](#es6-array-spreads) -1. [Let and Const](#references) -1. [Iterators and Generators](#iterators-and-generators) -1. [Modules](#modules) +1. [数组 Spreads](#es6-array-spreads) +1. [Let 及 Const](#references) +1. [迭代器和生成器](#iterators-and-generators) +1. [模块](#modules) -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## Testing + +## 测试 - [28.1](#28.1) **Yup.** @@ -1926,10 +1941,10 @@ } ``` -**[⬆ back to top](#table-of-contents)** - +**[⬆ 返回目录](#table-of-contents)** -## Performance + +## 性能 - [On Layout & Web Performance](http://kellegous.com/j/2013/01/26/layout-performance/) - [String vs Array Concat](http://jsperf.com/string-vs-array-concat/2) @@ -1938,45 +1953,46 @@ - [jQuery Find vs Context, Selector](http://jsperf.com/jquery-find-vs-context-sel/13) - [innerHTML vs textContent for script text](http://jsperf.com/innerhtml-vs-textcontent-for-script-text) - [Long String Concatenation](http://jsperf.com/ya-string-concat) - - Loading... + - [Are Javascript functions like `map()`, `reduce()`, and `filter()` optimized for traversing arrays?](https://www.quora.com/JavaScript-programming-language-Are-Javascript-functions-like-map-reduce-and-filter-already-optimized-for-traversing-array/answer/Quildreen-Motta) + - 等等... -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + +## 相关资源(英文) -## Resources +**了解 ES6** -**Learning ES6** - - - [Draft ECMA 2015 (ES6) Spec](https://people.mozilla.org/~jorendorff/es6-draft.html) + - [ECMA 2015 (ES6) 规范草案](https://people.mozilla.org/~jorendorff/es6-draft.html) - [ExploringJS](http://exploringjs.com/) - - [ES6 Compatibility Table](https://kangax.github.io/compat-table/es6/) - - [Comprehensive Overview of ES6 Features](http://es6-features.org/) + - [ES6 兼容性表](https://kangax.github.io/compat-table/es6/) + - [ES6 特性全面概况](http://es6-features.org/) -**Read This** +**看看这个** - [Annotated ECMAScript 5.1](http://es5.github.com/) -**Tools** +**工具** - - Code Style Linters + - 代码风格检查器(Lint) + [ESlint](http://eslint.org/) - [Airbnb Style .eslintrc](https://github.com/airbnb/javascript/blob/master/linters/.eslintrc) + [JSHint](http://www.jshint.com/) - [Airbnb Style .jshintrc](https://github.com/airbnb/javascript/blob/master/linters/jshintrc) + [JSCS](https://github.com/jscs-dev/node-jscs) - [Airbnb Style Preset](https://github.com/jscs-dev/node-jscs/blob/master/presets/airbnb.json) -**Other Styleguides** +**其他风格指南** - [Google JavaScript Style Guide](http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml) - [jQuery Core Style Guidelines](http://docs.jquery.com/JQuery_Core_Style_Guidelines) - [Principles of Writing Consistent, Idiomatic JavaScript](https://github.com/rwldrn/idiomatic.js/) -**Other Styles** +**其他风格** - [Naming this in nested functions](https://gist.github.com/4135065) - Christian Johansen - [Conditional Callbacks](https://github.com/airbnb/javascript/issues/52) - Ross Allen - [Popular JavaScript Coding Conventions on Github](http://sideeffect.kr/popularconvention/#javascript) - JeongHoon Byun - [Multiple var statements in JavaScript, not superfluous](http://benalman.com/news/2012/05/multiple-var-statements-javascript/) - Ben Alman -**Further Reading** +**拓展阅读** - [Understanding JavaScript Closures](http://javascriptweblog.wordpress.com/2010/10/25/understanding-javascript-closures/) - Angus Croll - [Basic JavaScript for the impatient programmer](http://www.2ality.com/2013/06/basic-javascript.html) - Dr. Axel Rauschmayer @@ -1984,7 +2000,7 @@ - [ES6 Features](https://github.com/lukehoban/es6features) - Luke Hoban - [Frontend Guidelines](https://github.com/bendc/frontend-guidelines) - Benjamin De Cock -**Books** +**书籍** - [JavaScript: The Good Parts](http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742) - Douglas Crockford - [JavaScript Patterns](http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752) - Stoyan Stefanov @@ -2002,7 +2018,7 @@ - [Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript](http://amzn.com/0321812182) - David Herman - [Eloquent JavaScript](http://eloquentjavascript.net/) - Marijn Haverbeke -**Blogs** +**博客** - [DailyJS](http://dailyjs.com/) - [JavaScript Weekly](http://javascriptweekly.com/) @@ -2016,78 +2032,116 @@ - [Dustin Diaz](http://dustindiaz.com/) - [nettuts](http://net.tutsplus.com/?s=javascript) -**Podcasts** +**播客** - [JavaScript Jabber](http://devchat.tv/js-jabber/) -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## In the Wild + +## 使用情况 - This is a list of organizations that are using this style guide. Send us a pull request or open an issue and we'll add you to the list. + 下列组织应用这份风格指南。 + - **3blades**: [3Blades/javascript](https://github.com/3blades/javascript) + - **4Catalyzer**: [4Catalyzer/javascript](https://github.com/4Catalyzer/javascript) - **Aan Zee**: [AanZee/javascript](https://github.com/AanZee/javascript) - **Adult Swim**: [adult-swim/javascript](https://github.com/adult-swim/javascript) - **Airbnb**: [airbnb/javascript](https://github.com/airbnb/javascript) - - **American Insitutes for Research**: [AIRAST/javascript](https://github.com/AIRAST/javascript) + - **AltSchool**: [AltSchool/javascript](https://github.com/AltSchool/javascript) - **Apartmint**: [apartmint/javascript](https://github.com/apartmint/javascript) + - **Ascribe**: [ascribe/javascript](https://github.com/ascribe/javascript) - **Avalara**: [avalara/javascript](https://github.com/avalara/javascript) + - **Avant**: [avantcredit/javascript](https://github.com/avantcredit/javascript) + - **Axept**: [axept/javascript](https://github.com/axept/javascript) + - **BashPros**: [BashPros/javascript](https://github.com/BashPros/javascript) - **Billabong**: [billabong/javascript](https://github.com/billabong/javascript) + - **Bisk**: [bisk/javascript](https://github.com/Bisk/javascript/) + - **Bonhomme**: [bonhommeparis/javascript](https://github.com/bonhommeparis/javascript) + - **Brainshark**: [brainshark/javascript](https://github.com/brainshark/javascript) + - **CaseNine**: [CaseNine/javascript](https://github.com/CaseNine/javascript) + - **Chartboost**: [ChartBoost/javascript-style-guide](https://github.com/ChartBoost/javascript-style-guide) + - **ComparaOnline**: [comparaonline/javascript](https://github.com/comparaonline/javascript-style-guide) - **Compass Learning**: [compasslearning/javascript-style-guide](https://github.com/compasslearning/javascript-style-guide) - **DailyMotion**: [dailymotion/javascript](https://github.com/dailymotion/javascript) + - **DoSomething**: [DoSomething/eslint-config](https://github.com/DoSomething/eslint-config) - **Digitpaint** [digitpaint/javascript](https://github.com/digitpaint/javascript) + - **Ecosia**: [ecosia/javascript](https://github.com/ecosia/javascript) - **Evernote**: [evernote/javascript-style-guide](https://github.com/evernote/javascript-style-guide) + - **Evolution Gaming**: [evolution-gaming/javascript](https://github.com/evolution-gaming/javascript) + - **EvozonJs**: [evozonjs/javascript](https://github.com/evozonjs/javascript) - **ExactTarget**: [ExactTarget/javascript](https://github.com/ExactTarget/javascript) - **Expensify** [Expensify/Style-Guide](https://github.com/Expensify/Style-Guide/blob/master/javascript.md) - **Flexberry**: [Flexberry/javascript-style-guide](https://github.com/Flexberry/javascript-style-guide) - **Gawker Media**: [gawkermedia/javascript](https://github.com/gawkermedia/javascript) - - **GeneralElectric**: [GeneralElectric/javascript](https://github.com/GeneralElectric/javascript) + - **General Electric**: [GeneralElectric/javascript](https://github.com/GeneralElectric/javascript) + - **Generation Tux**: [GenerationTux/javascript](https://github.com/generationtux/styleguide) - **GoodData**: [gooddata/gdc-js-style](https://github.com/gooddata/gdc-js-style) - **Grooveshark**: [grooveshark/javascript](https://github.com/grooveshark/javascript) - - **How About We**: [howaboutwe/javascript](https://github.com/howaboutwe/javascript) - - **InfoJobs**: [InfoJobs/JavaScript-Style-Guide](https://github.com/InfoJobs/JavaScript-Style-Guide) - - **Intent Media**: [intentmedia/javascript](https://github.com/intentmedia/javascript) + - **Honey**: [honeyscience/javascript](https://github.com/honeyscience/javascript) + - **How About We**: [howaboutwe/javascript](https://github.com/howaboutwe/javascript-style-guide) + - **Huballin**: [huballin/javascript](https://github.com/huballin/javascript) + - **HubSpot**: [HubSpot/javascript](https://github.com/HubSpot/javascript) + - **Hyper**: [hyperoslo/javascript-playbook](https://github.com/hyperoslo/javascript-playbook/blob/master/style.md) + - **InterCity Group**: [intercitygroup/javascript-style-guide](https://github.com/intercitygroup/javascript-style-guide) - **Jam3**: [Jam3/Javascript-Code-Conventions](https://github.com/Jam3/Javascript-Code-Conventions) + - **JeopardyBot**: [kesne/jeopardy-bot](https://github.com/kesne/jeopardy-bot/blob/master/STYLEGUIDE.md) - **JSSolutions**: [JSSolutions/javascript](https://github.com/JSSolutions/javascript) - - **Kinetica Solutions**: [kinetica/javascript](https://github.com/kinetica/javascript) + - **KickorStick**: [kickorstick/javascript](https://github.com/kickorstick/javascript) + - **Kinetica Solutions**: [kinetica/javascript](https://github.com/kinetica/Javascript-style-guide) + - **Lonely Planet**: [lonelyplanet/javascript](https://github.com/lonelyplanet/javascript) + - **M2GEN**: [M2GEN/javascript](https://github.com/M2GEN/javascript) - **Mighty Spring**: [mightyspring/javascript](https://github.com/mightyspring/javascript) - **MinnPost**: [MinnPost/javascript](https://github.com/MinnPost/javascript) + - **MitocGroup**: [MitocGroup/javascript](https://github.com/MitocGroup/javascript) - **ModCloth**: [modcloth/javascript](https://github.com/modcloth/javascript) - **Money Advice Service**: [moneyadviceservice/javascript](https://github.com/moneyadviceservice/javascript) - **Muber**: [muber/javascript](https://github.com/muber/javascript) - **National Geographic**: [natgeo/javascript](https://github.com/natgeo/javascript) - - **National Park Service**: [nationalparkservice/javascript](https://github.com/nationalparkservice/javascript) - **Nimbl3**: [nimbl3/javascript](https://github.com/nimbl3/javascript) + - **Nulogy**: [nulogy/javascript](https://github.com/nulogy/javascript) + - **Orange Hill Development**: [orangehill/javascript](https://github.com/orangehill/javascript) - **Orion Health**: [orionhealth/javascript](https://github.com/orionhealth/javascript) + - **OutBoxSoft**: [OutBoxSoft/javascript](https://github.com/OutBoxSoft/javascript) - **Peerby**: [Peerby/javascript](https://github.com/Peerby/javascript) - **Razorfish**: [razorfish/javascript-style-guide](https://github.com/razorfish/javascript-style-guide) - **reddit**: [reddit/styleguide/javascript](https://github.com/reddit/styleguide/tree/master/javascript) - - **REI**: [reidev/js-style-guide](https://github.com/reidev/js-style-guide) + - **React**: [facebook.github.io/react/contributing/how-to-contribute.html#style-guide](https://facebook.github.io/react/contributing/how-to-contribute.html#style-guide) + - **REI**: [reidev/js-style-guide](https://github.com/rei/code-style-guides/blob/master/docs/javascript.md) - **Ripple**: [ripple/javascript-style-guide](https://github.com/ripple/javascript-style-guide) - **SeekingAlpha**: [seekingalpha/javascript-style-guide](https://github.com/seekingalpha/javascript-style-guide) - **Shutterfly**: [shutterfly/javascript](https://github.com/shutterfly/javascript) - - **StudentSphere**: [studentsphere/javascript](https://github.com/studentsphere/javascript) + - **Sourcetoad**: [sourcetoad/javascript](https://github.com/sourcetoad/javascript) + - **Springload**: [springload/javascript](https://github.com/springload/javascript) + - **StratoDem Analytics**: [stratodem/javascript](https://github.com/stratodem/javascript) + - **SteelKiwi Development**: [steelkiwi/javascript](https://github.com/steelkiwi/javascript) + - **StudentSphere**: [studentsphere/javascript](https://github.com/studentsphere/guide-javascript) + - **SwoopApp**: [swoopapp/javascript](https://github.com/swoopapp/javascript) + - **SysGarage**: [sysgarage/javascript-style-guide](https://github.com/sysgarage/javascript-style-guide) + - **Syzygy Warsaw**: [syzygypl/javascript](https://github.com/syzygypl/javascript) - **Target**: [target/javascript](https://github.com/target/javascript) - **TheLadders**: [TheLadders/javascript](https://github.com/TheLadders/javascript) + - **The Nerdery**: [thenerdery/javascript-standards](https://github.com/thenerdery/javascript-standards) - **T4R Technology**: [T4R-Technology/javascript](https://github.com/T4R-Technology/javascript) - - **Userify**: [userify/javascript](https://github.com/userify/javascript) - **VoxFeed**: [VoxFeed/javascript-style-guide](https://github.com/VoxFeed/javascript-style-guide) + - **WeBox Studio**: [weboxstudio/javascript](https://github.com/weboxstudio/javascript) - **Weggo**: [Weggo/javascript](https://github.com/Weggo/javascript) - **Zillow**: [zillow/javascript](https://github.com/zillow/javascript) - **ZocDoc**: [ZocDoc/javascript](https://github.com/ZocDoc/javascript) -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** -## Translation + +## 翻译 - This style guide is also available in other languages: + 这份风格指南也有其他语言的译本: - ![br](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Brazilian Portuguese**: [armoucar/javascript-style-guide](https://github.com/armoucar/javascript-style-guide) - ![bg](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Bulgaria.png) **Bulgarian**: [borislavvv/javascript](https://github.com/borislavvv/javascript) - ![ca](https://raw.githubusercontent.com/fpmweb/javascript-style-guide/master/img/catala.png) **Catalan**: [fpmweb/javascript-style-guide](https://github.com/fpmweb/javascript-style-guide) - ![tw](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Taiwan.png) **Chinese(Traditional)**: [jigsawye/javascript](https://github.com/jigsawye/javascript) - - ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese(Simplified)**: [sivan/javascript-style-guide](https://github.com/sivan/javascript-style-guide) + - ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese(Simplified)**: [yuche/javascript](https://github.com/yuche/javascript) - ![fr](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/France.png) **French**: [nmussy/javascript-style-guide](https://github.com/nmussy/javascript-style-guide) - ![de](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Germany.png) **German**: [timofurrer/javascript-style-guide](https://github.com/timofurrer/javascript-style-guide) - ![it](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Italy.png) **Italian**: [sinkswim/javascript-style-guide](https://github.com/sinkswim/javascript-style-guide) @@ -2098,20 +2152,23 @@ - ![es](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Spain.png) **Spanish**: [paolocarrasco/javascript-style-guide](https://github.com/paolocarrasco/javascript-style-guide) - ![th](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Thailand.png) **Thai**: [lvarayut/javascript-style-guide](https://github.com/lvarayut/javascript-style-guide) -## The JavaScript Style Guide Guide + +## JavaScript 编码规范说明 - [Reference](https://github.com/airbnb/javascript/wiki/The-JavaScript-Style-Guide-Guide) -## Chat With Us About JavaScript + +## 讨论 JavaScript - - Find us on [gitter](https://gitter.im/airbnb/javascript). + - 欢迎到 [gitter](https://gitter.im/airbnb/javascript) 与我们聊天(英文)。 -## Contributors +## 贡献者 - - [View Contributors](https://github.com/airbnb/javascript/graphs/contributors) + - [查看原始项目贡献者](https://github.com/airbnb/javascript/graphs/contributors) + - [查看简中翻译贡献者](https://github.com/yuche/javascript/graphs/contributors) -## License +## 许可协议 (The MIT License) @@ -2136,6 +2193,10 @@ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -**[⬆ back to top](#table-of-contents)** +**[⬆ 返回目录](#table-of-contents)** + +## 修订 + +我们鼓励您派生本指南和更改规则以适应您的团队需求。您可以在下方列出对本风格指南的修改,以便定期更新本指南而无需处理合并冲突。 # };