javascript

JavaScript 风格指南(基于Airbnb)

JavaScript最合理的方法 A mostly reasonable approach to JavaScript

注意: 这个指南假定你正在使用Babel, 并且需要你使用或等效的使用babel-preset-airbnb。 同时假定你在你的应用里安装了带有或等效的airbnb-browser-shimsshims/polyfills

目录

  1. Types
  2. References
  3. Objects
  4. Arrays
  5. Destructuring
  6. Strings
  7. Functions
  8. Arrow Functions
  9. Classes & Constructors
  10. Modules
  11. Iterators and Generators
  12. Properties
  13. Variables
  14. Hoisting
  15. Comparison Operators & Equality
  16. Blocks
  17. Control Statements
  18. Comments
  19. Whitespace
  20. Commas
  21. Semicolons
  22. Type Casting & Coercion
  23. Naming Conventions
  24. Accessors
  25. Events
  26. jQuery
  27. ECMAScript 5 Compatibility
  28. ECMAScript 6+ (ES 2015+) Styles
  29. Standard Library
  30. Testing
  31. Performance
  32. Resources
  33. The JavaScript Style Guide Guide

Types

⬆ back to top

References

⬆ back to top

Objects

  // very bad
  const original = { a: 1, b: 2 };
  const copy = Object.assign(original, { c: 3 }); // this mutates `original` ಠ_ಠ
  delete copy.a; // so does this

  // bad
  const original = { a: 1, b: 2 };
  const copy = Object.assign({}, original, { c: 3 }); // copy => { a: 1, b: 2, c: 3 }

  // good es6扩展运算符 ...
  const original = { a: 1, b: 2 };
  // 浅拷贝
  const copy = { ...original, c: 3 }; // copy => { a: 1, b: 2, c: 3 }

  // rest 赋值运算符
  const { a, ...noA } = copy; // noA => { b: 2, c: 3 }

⬆ back to top

Arrays

⬆ back to top

Destructuring

⬆ back to top

Strings

⬆ back to top

Functions

⬆ back to top

Arrow Functions

⬆ back to top

Classes & Constructors

⬆ back to top

Modules

⬆ back to top

Iterators and Generators

⬆ back to top

Properties

⬆ back to top

Variables

⬆ back to top

Hoisting

⬆ back to top

Comparison Operators & Equality

⬆ back to top

Blocks

⬆ back to top

Control Statements

⬆ back to top

Comments

⬆ back to top

Whitespace

⬆ back to top

Commas

⬆ back to top

Semicolons

⬆ back to top

Type Casting & Coercion

⬆ back to top

Naming Conventions

⬆ back to top

Accessors

⬆ back to top

Events

⬆ back to top

jQuery

⬆ back to top

ES5 兼容性

⬆ back to top

ECMAScript 6+ (ES 2015+) Styles

  1. 箭头函数——Arrow Functions
  2. 类——Classes
  3. 对象缩写——Object Shorthand
  4. 对象简写——Object Concise
  5. 对象计算属性——Object Computed Properties
  6. 模板字符串——Template Strings
  7. 解构赋值——Destructuring
  8. 默认参数——Default Parameters
  9. Rest
  10. Array Spreads
  11. Let and Const
  12. 幂操作符——Exponentiation Operator
  13. 迭代器和生成器——Iterators and Generators
  14. 模块——Modules

⬆ back to top

Standard Library

标准库中包含一些功能受损但是由于历史原因遗留的工具类

Testing

⬆ back to top

Performance

⬆ back to top

Resources

Learning ES6

Read This

Tools

Other Style Guides

Other Styles

Further Reading

Books

Blogs

Podcasts

⬆ back to top

The JavaScript Style Guide Guide