前端代码规范整理

朴灿烈づ我的快乐病毒、 2023-07-02 06:29 179阅读 0赞

前端代码规范

以下内容来源于airbnb,竹春翻译

数据类型

  • 1.1 基本数据类型使用得时候直接进行赋值。

    • string
    • number
    • boolean
    • null
    • undefined
    • symbol

    const foo = 1;
    let bar = foo;
    bar = 9;
    conso.log(foo, bar); // => 1, 9

Symbol的支持性并不是很好,所以应当在支持他们的浏览器中使用。

  • 1.2 复杂数据类型需要对他的值进行引用。

    • object
    • array
    • function

    const foo = [1, 2];
    const bar = foo;
    bar[0] = 9;
    console.log(foo[0], bar[0]); // => 9, 9
    // 复杂数据类型引用的时候应该考虑深拷贝,防止改变原数据。

声明

  • 2.1 尽量使用const声明来代替var

这样可以避免重复声明产生一些不可预知的错误。

  1. const a = 1;
  2. const b = 2;
  • 2.2 如果必须重复声明,请使用let代替var

let属于块级作用域比函数作用域中的var更加合适。

  1. let count = 1;
  2. if (true) {
  3. count += 1;
  4. }
  • 2.3 letconst都有自己的作用域。

    {
    let a = 1;
    const b = 1;
    }
    console.log(a); //ReferenceError 引用抛错

对象

  • 3.1 使用字面量方式而非创建对象。

    const item = {};

  • 3.2 在创建具有动态属性名对象的时候应当在创建的时候赋予动态属性。

这样可以保证你在一个地方定义对象所有的属性。

  1. const obj = {
  2. id: 5,
  3. name: 'San Francisco',
  4. [getKey('enabled')]: true,
  5. }
  • 3.3 对象的方法使用简写。

    const atom = {
    value: 1,

    1. addValue(value) {
    2. return atom.value + value;
    3. }

    }

  • 3.4 属性值使用简写。

简短且描述性更强。

  1. const lukeSkywalker = 'Luke Skywalker';
  2. const obj = {
  3. lukeSkywalker,
  4. }
  • 3.5 在对象声明的时候应当将简写的属性放在最前。

为了更容易分辨哪一个是简写。

  1. const anakinSkywalker = 'Anakin Skywalker';
  2. const lukeSkywalker = 'Luke Skywalker';
  3. const obj = {
  4. lukeSkywalker,
  5. anakinSkywalker,
  6. episodeOne: 1,
  7. twoJediWalkInt: 2,
  8. three: 3,
  9. }
  • 3.6 只给非法的属性添加标示符。

通常是为了提高可读性。带有标示符之后高亮显示,提高JS性能。

  1. const good = {
  2. foo: 3,
  3. bar: 4,
  4. 'data-blah': 5,
  5. }
  • 3.7 不要直接使用Object.prototype(对象原型)方法,比如hasOwnPropertypropertyIsEnumerable,和isPrototypeOf

因为这些方法可能对对象产生影响。

  1. const has = Object.prototype.hasOwnProperty; // in module scope.
  2. import has from 'has'
  3. console.log(has.call(object, key));
  • 3.8 使用对象扩展符来代替Object.assign进行浅拷贝,同时可以省略不需要的属性。

    const original = { a: 1, b: 2};
    const copy = { …original, c: 3}; // copy => { a: 1, b: 2, c: 3 };
    const { a, …noA} = copy; // noA => { b: 2, c: 3 };

数组

  • 4.1 使用字面语法创建数组。

    const items = [];

  • 4.2 在数组中添加项请使用数组的push方法而非直接操作数组。

    const someStack = [];
    smeStack.push(‘one’);

  • 4.3 拷贝数组请使用...

    const items = [ 1, 2, 3 ];
    const itemsCopy = […items]; // …只能进行浅拷贝

  • 4.4 转换一个可迭代的对象请使用...而不是Array.from

    const foo = document.querySelectorAll(’.foo’);
    const nodes = […foo];

  • 4.5 转换类似于数组的对象请使用Array.from

    const arrLike = { 0: ‘foo’, 1: ‘bar’ };
    const arr = Array.from(arrLike);

  • 4.6 请使用Array.from代替...进行操作,它避免了创建中间数组。

    const baz = Array.from(foo, bar); // 相当于 […foo].map(bar);

  • 4.7 数组的回调方法请书写返回值,如果函数体是由一条表达式组成且无副作用可以省略。

    // good
    [1, 2, 3].map(x => x + 1);
    // good
    [[0, 1], [2, 3]].reduce((acc, item, index) => {
    const flatten = acc.concat(item);
    acc[index] = flatten;
    return flatten;
    })
    // good
    [[0, 1], [2, 3], [4, 5]].reduce((acc, item, index) => {
    const flatten = acc.concat(item);
    return flatten;
    });
    // bad
    inbox.filter((msg) => {
    const { subject, author } = msg;
    if (subject === ‘Mockingbird’) {
    return author === ‘Harper Lee’;
    } else {
    return false;
    }
    });
    // good
    inbox.filter((msg) => {
    const { subject, author } = msg;
    if (subject === ‘Mockingbird’) {
    return author === ‘Harper Lee’;
    }
    return false;
    });

  • 4.8 如果数组有多行,则在打开数组括号后和关闭数组括号前使用换行符。

    // good
    const arr = [[0, 1], [2, 3], [4, 5]];
    const objectInArray = [
    {
    id: 1,
    },
    {
    id: 2,
    },
    ];
    const numberInArray = [
    1,
    2,
    ];

结构赋值

  • 5.1 在访问和使用对象的多个属性时可以使用结构赋值。

结构赋值可以避免创建临时性的引用。

  1. // bad
  2. function getFullName(user) {
  3. const firstName = user.firstName;
  4. const lastName = user.lastName;
  5. return `${firstName} ${lastName}`;
  6. }
  7. // best
  8. function getFullName({ firstName, lastName }) {
  9. return `${firstName} ${lastName}`;
  10. }
  • 5.2 数组使用结构赋值。

    const arr = [1, 2, 3, 4];
    // bad
    const first = arr[0];
    const second = arr[1];
    // good
    const [first, second] = arr;

  • 5.3 多个返回值使用对象返回,而非数组。

为了在未来添加属性的时候不需要为了改变数组顺序而调整代码。

  1. // bad
  2. function processInput(input) {
  3. return [left, right, top, bottom];
  4. }
  5. // good
  6. function processInput(input) {
  7. return { left, right, top, bottom };
  8. }
  9. const { left, top } = processInput(input);

字符串

  • 6.1 字符串应该使用单引号。

    // good
    const name = ‘Capt. Janeway’;

  • 6.2 字符串不应该换行。

影响代码美观和不利于搜索。

  1. // good
  2. const str = 'ddd ddd ddd ddd ddd ddd ddd ddd ddd ddd';
  • 6.3 当需要编译字符串的时候请使用字符串模板代替字符拼接。

字符串模板提供了更加简洁的语法,换行与插值。

  1. // bad
  2. function sayHi(name) {
  3. return `How are you,${ name }?`;
  4. }
  5. // good
  6. function sayHi(name) {
  7. return `how are you,${name}?`;
  8. }
  • 6.4 不要给eval()传递一个字符串,会导致太多的漏洞。
  • 6.5 不必要的字符串不用转义。

反斜杠会影响代码可读性,所以只在他们应该出现的时候使用。

函数

  • 7.1 使用命名函数表达式而非声明函数。

函数声明会被预解析,所以很容易在这个函数还没有被赋值之前使用,这会影响代码的可读性和维护性。如果你发现一个函数的定义太大或者太复杂以至于影响到了理解文件的其余部分,可以将其提取到模块中去。不要忘记给函数命名,不管是否能够从局部变量中推断出该名称。这样做可以尽可能的减少错误。

  1. // bad
  2. const foo = function () {
  3. // ...
  4. }
  5. // good
  6. const short = function longUniquMoreDescriptiveLexicalFoo() {
  7. // ...
  8. }
  • 7.2 将立即执行函数用圆括号包裹起来。

立即执行函数是一个独立的个体,用括号包裹起来可以清晰的看到调用。在一个到处都是模块的世界,你几乎不需要一个立即执行函数。

  1. (function () {
  2. console.log('Welcome to the Internet.Please follow me');
  3. }())
  • 7.3 不要把函数声明在一个功能模块中(if,while,等等)。函数可以赋值给变量。浏览器允许你这样做,但是各种浏览器解析出来的结果都是不同的,所以这并不是一个好消息。
  • 7.4 ECMA-262将一个代码块定义为一个语句列表,函数表达式不是语句列表。

    // bad
    if (currentUser) {
    function test() {
    console.log(‘Nope.’);
    }
    }
    // good
    let test;
    if (currentUser) {
    test = () => {
    console.log(‘Yup.’);
    };
    }

  • 7.5 永远不要指定参数为arguments.这将优先于为每个函数范围提供的arguments对象。

    // bad
    function foo(name, options, arguments) {
    // …
    }
    // good
    function foo(name, options, args) {
    // …
    }

  • 7.6 不要使用arguments,使用...代替。

因为...可以明确得到需要的参数,另外通过该语法得到的是真正的数组而非类数组。

  1. // bad
  2. function concatenateAll() {
  3. const args = Array.prototype.slice.call(arguments);
  4. return args.join('');
  5. }
  6. // good
  7. function concatenateAll(...args) {
  8. return args.join('');
  9. }
  • 7.7 给参数设置默认值而不是改变函数的参数。

    // bad
    function handleThings(opts) {
    opts = opts || {}
    // …
    }
    // still bad
    function handleThings(opts) {
    if(opts === void 0){
    opts ={}
    }
    }
    // good
    function handleThings(opts = {}){
    // …
    }

  • 7.8 避免使用默认值带来的副作用。

因为结果难以预测。

  1. var b = 1;
  2. // bad
  3. function count(a = b++) {
  4. console.log(a);
  5. }
  6. count(); // 1
  7. count(); // 2
  8. count(3); // 3
  9. count(); // 3
  • 7.9 始终将默认参数放在最后。

    // bad
    function handleThings(opts = {}, name) {
    // …
    }
    // good
    function handleThings(name, opts = {}){
    // …
    }

  • 7.10 永远不要用构造函数去创建一个新的函数。

以这种方式创建函数其中的某些参数类似于eval(),将会产生漏洞。

  1. // bad
  2. var add = new Function('a', 'b', 'return a + b');
  3. // still bad
  4. var subtract = Function('a', 'b', 'return a - b');
  • 7.11 函数命名之前记得加空格。

当不需要一个函数命名或需要删除一个函数名字的时候仍然能够保持一致性。

  1. // bad
  2. const f = function(){}
  3. const g = function (){};
  4. const h = function() {};
  5. // good
  6. const x = function () {};
  7. const y = function a() {};
  • 7.12 不要改变参数。

对参数的改变很可能造成对原始数据的改变,造成不必要的麻烦。

  1. // bad
  2. function f1(obj) {
  3. obj.key = 1;
  4. }
  5. // good
  6. function f2(obj) {
  7. const key = Object.prototype.hasOwnProperty.call(obj,'key')?obj.key:1;
  8. }
  • 7.13 永远不要给参数重新赋值。

重新给参数赋值可能会导致一些意想不到的结果,当访问arguments的时候。而且可能会影响性能,尤其是在v8引擎下。

  1. // bad
  2. function f1(a) {
  3. a = 1;
  4. // ...
  5. }
  6. function f2(a) {
  7. if (!a) { a = 1; }
  8. // ...
  9. }
  10. // good
  11. function f3(a) {
  12. const b = a || 1;
  13. // ...
  14. }
  15. function f4(a = 1) {
  16. // ...
  17. }
  • 7.14 使用...来代替函数改变this指向。

因为他相当简洁,不需要别的上下文,也不需要使用新的apply

  1. // bad
  2. const x= [1, 2, 3, 4, 5];
  3. console.log.apply(console, x);
  4. // good
  5. const x = [1, 2, 3, 4, 5];
  6. console.log(...x);
  7. // good
  8. new Date(...[2016, 8, 5]);

箭头函数

  • 8.1 当必须使用匿名函数的时候请使用箭头函数。

箭头函数创建了单独的作用域,语法上更加简洁。

  1. // bad
  2. [1, 2, 3].map(function (x) {
  3. const y = x + 1;
  4. return x * y;
  5. });
  6. // good
  7. [1, 2, 3].map((x) => {
  8. const y = x + 1;
  9. return x * y;
  10. });
  • 8.2 如果一个函数体由一条语句组成且这条语句返回一个没有副作用的表达式,可以将大括号省略进行隐式返回,反之,保留大括号并使用return语句进行返回。

提高代码可读性。

  1. // bad
  2. [1, 2, 3].map(number => {
  3. const nextNumber = number + 1;
  4. `A string containing the ${nextNumber}.`;
  5. });
  6. // good
  7. [1, 2, 3].map(number => `A string containing the ${number + 1}.`);
  • 8.3 如果表达式有多行,请用圆括号包裹起来提高可读性。

区分整体函数。

  1. // bad
  2. ['get', 'post', 'put'].map(httpMethod => Object.prototype.hasOwnProperty.call(
  3. httpMagicObjectWithAVeryLongName,
  4. httpMethod,
  5. )
  6. );
  7. // good
  8. ['get', 'post', 'put'].map(httpMethod => (
  9. Object.prototype.hasOwnProperty.call(
  10. httpMagicObjectWithAVeryLongName,
  11. httpMethod,
  12. )
  13. ));
  • 8.4 如果一个函数只有一个参数且省略了函数体的大括号,那么参数的圆括号也省略。否则,请不要省略任何参数,保持清晰和一致性。注意:小括号不省略,也是可以的。

为了代码看起来更清晰。

  1. // bad
  2. [1, 2, 3].map((x) => x * x);
  3. // good
  4. [1, 2, 3].map(x => x * x);
  5. // good
  6. [1, 2, 3].map(number => (
  7. `A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
  8. ));
  9. // bad
  10. [1, 2, 3].map(x => {
  11. const y = x + 1;
  12. return x * y;
  13. });
  14. // good
  15. [1, 2, 3].map((x) => {
  16. const y = x + 1;
  17. return x * y;
  18. });
  • 8.5 避免混淆箭头函数=>和比较操作符(<=,>=)。

    // bad
    const itemHeight = item => item.height <= 256 ? item.largeSize : item.smallSize;
    // bad
    const itemHeight = (item) => item.height >= 256 ? item.largeSize : item.smallSize;
    // good
    const itemHeight = item => (item.height <= 256 ? item.largeSize : item.smallSize);
    // good
    const itemHeight = (item) => {
    const { height, largeSize, smallSize } = item;
    return height <= 256 ? largeSize : smallSize;
    };

  • 8.6 使用隐式返回的情况下请强调函数体的位置。

    // bad
    foo =>
    // bar;
    foo =>
    (bar);
    // good
    foo => bar;
    foo => (bar);
    foo => (
    bar
    )

类和构造函数

  • 9.1 尽可能多的使用class,避免直接操作原型。

class更简洁和便于推理。

  1. // bad
  2. function Queue(contents = []) {
  3. this.queue = [...contents];
  4. }
  5. Queue.prototype.pop = function () {
  6. const value = this.queue[0];
  7. this.queue.splice(0, 1);
  8. return value;
  9. };
  10. // good
  11. class Queue {
  12. constructor(contents = []) {
  13. this.queue = [...contents];
  14. }
  15. pop() {
  16. const value = this.queue[0];
  17. this.queue.splice(0, 1);
  18. return value;
  19. }
  20. }
  • 9.2 使用extends代替继承。

这是一种内置的方法,可以在不破坏instanceof的情况下继承原型。

  1. // bad
  2. const inherits = require('inherits');
  3. function PeekableQueue(contents) {
  4. Queue.apply(this, contents);
  5. }
  6. inherits(PeekableQueue, Queue);
  7. PeekableQueue.prototype.peek = function () {
  8. return this.queue[0];
  9. };
  10. // good
  11. class PeekableQueue extends Queue {
  12. peek() {
  13. return this.queue[0];
  14. }
  15. }
  • 9.3 方法可以返回this来进行链式调用。

    // bad
    Jedi.prototype.jump = function () {
    this.jumping = true;
    return true;
    };
    Jedi.prototype.setHeight = function (height) {
    this.height = height;
    };
    const luke = new Jedi();
    luke.jump(); // => true
    luke.setHeight(20); // => undefined
    // good
    class Jedi {
    jump() {
    this.jumping = true;
    return this;
    }
    setHeight(height) {
    this.height = height;
    return this;
    }
    }
    const luke = new Jedi();
    luke.jump()
    .setHeight(20);

  • 9.4 编写一个自定义的toString方法也是可以的,只要能保证没有副作用且可以正常运行。

    class Jedi {
    constructor(options = {}) {
    this.name = options.name || ‘no name’;
    }
    getName() {
    return this.name;
    }
    toString() {
    return Jedi - ${this.getName()};
    }
    }

  • 9.5 如果没有指定默认构造函数,则类具有默认构造函数。空构造函数或只委托给父类的构造函数是不必要的。

    // bad
    class Jedi {
    constructor() {}
    getName() {
    return this.name;
    }
    }
    // bad
    class Rey extends Jedi {
    constructor(…args) {
    super(…args);
    }
    }
    // good
    class Rey extends Jedi {
    constructor(…args) {
    super(…args);
    this.name = ‘Rey’;
    }
    }

  • 9.6 避免重复的类。

重复的类成员声明会默默地选择最后一个——几乎可以肯定,重复是一个错误。

  1. // bad
  2. class Foo {
  3. bar() { return 1; }
  4. bar() { return 2; }
  5. }
  6. // good
  7. class Foo {
  8. bar() { return 1; }
  9. }
  10. // good
  11. class Foo {
  12. bar() { return 2; }
  13. }

模块

  • 10.1 始终在非标准模块中使用(import/export),可以随意导出需要的任意模块。

模块是未来的趋势,我们可以先尝试使用。

  1. // bad
  2. const AirbnbStyleGuide = require('./AirbnbStyleGuide');
  3. module.exports = AirbnbStyleGuide.es6;
  4. // ok
  5. import AirbnbStyleGuide from './AirbnbStyleGuide';
  6. export default AirbnbStyleGuide.es6;
  7. // best
  8. import { es6 } from './AirbnbStyleGuide';
  9. export default es6;
  • 10.2 不要使用通配符导出。

除非确认只有一个默认导出。

  1. // bad
  2. import * as AirbnbStyleGuide from './AirbnbStyleGuide';
  3. // good
  4. import AirbnbStyleGuide from './AirbnbStyleGuide';
  • 10.3 不要直接从导入导出。

尽管一行代码很简洁,但是有一种清晰的导入和导出方式可以使事情保持一致。

  1. // bad
  2. // filename es6.js
  3. export { es6 as default } from './AirbnbStyleGuide';
  4. // good
  5. // filename es6.js
  6. import { es6 } from './AirbnbStyleGuide';
  7. export default es6;
  • 10.4 如果需要同一个路径的多个方法,请从一个路径导出。

便于代码维护。

  1. // bad
  2. import foo from 'foo';
  3. // … some other imports … //
  4. import { named1, named2 } from 'foo';
  5. // good
  6. import foo, { named1, named2 } from 'foo';
  7. // good
  8. import foo, {
  9. named1,
  10. named2,
  11. } from 'foo';
  • 10.5 不要导出变量。

一般情况下应该只导出常量,避免对其他造成影响。

  1. // bad
  2. let foo = 3;
  3. export { foo };
  4. // good
  5. const foo = 3;
  6. export { foo };
  • 10.6 如果只导出一个方法,请使用默认值。

只导出一个方法是利于可读性和可维护性的,也是值得提倡的。

  1. // bad
  2. export function foo() {}
  3. // good
  4. export default function foo() {}
  • 10.7 将所有导入置顶。

都放在顶部可以防止意外情况。

  1. // bad
  2. import foo from 'foo';
  3. foo.init();
  4. import bar from 'bar';
  5. // good
  6. import foo from 'foo';
  7. import bar from 'bar';
  8. foo.init();
  • 10.8 多行导入应该缩进,和多行数组一样。

缩进规则同{},一样。

  1. // bad
  2. import {longNameA, longNameB, longNameC, longNameD, longNameE} from 'path';
  3. // good
  4. import {
  5. longNameA,
  6. longNameB,
  7. longNameC,
  8. longNameD,
  9. longNameE,
  10. } from 'path';
  • 10.9 模块语法中不允许webpack加载语法。

和webpack耦合度太高,更好的办法是直接使用webpack.config.js

  1. // bad
  2. import fooSass from 'css!sass!foo.scss';
  3. import barCss from 'style!css!bar.css';
  4. // good
  5. import fooSass from 'foo.scss';
  6. import barCss from 'bar.css';

迭代器和Generators

  • 11.1 不要使用迭代器。js中有更好的高阶函数可以代替for in ,for of

强制了不可变规则,处理返回值的纯函数比处理副作用更容易推理。

使用map()/every()/filter()/find()/findIndex()/reduce()/some()/...进行数组的遍历,先使用Object.keys()/Object.values()/Object.entries()对象生成数组,之后进行遍历。

  1. const numbers = [1, 2, 3, 4, 5];
  2. // bad
  3. let sum = 0;
  4. for (let num of numbers) {
  5. sum += num;
  6. }
  7. sum === 15;
  8. // good
  9. let sum = 0;
  10. numbers.forEach((num) => {
  11. sum += num;
  12. });
  13. sum === 15;
  14. // best (use the functional force)
  15. const sum = numbers.reduce((total, num) => total + num, 0);
  16. sum === 15;
  17. // good --> 不改变原数组 如果都符合条件 返回true 有一个不符合就返回false
  18. let arr = [14, 15, 16];
  19. arr.every(item=>{
  20. return item>15
  21. })
  22. // bad
  23. const increasedByOne = [];
  24. for (let i = 0; i < numbers.length; i++) {
  25. increasedByOne.push(numbers[i] + 1);
  26. }
  27. // good
  28. const increasedByOne = [];
  29. numbers.forEach((num) => {
  30. increasedByOne.push(num + 1);
  31. });
  32. // best (keeping it functional)
  33. const increasedByOne = numbers.map(num => num + 1);
  • 11.2 不要使用生成器(generators)。

因为不能很好的支持ES5。

  • 11.3 如果你必须使用生成器或忽略我们的建议,请使用正确的标示。

因为function*是同一个概念关键字的一部分——*不是function的修饰语,function*是一个独特的结构,不同于function

  1. // bad
  2. function * foo() {
  3. // ...
  4. }
  5. // bad
  6. const bar = function * () {
  7. // ...
  8. };
  9. // bad
  10. const baz = function *() {
  11. // ...
  12. };
  13. // bad
  14. const quux = function*() {
  15. // ...
  16. };
  17. // bad
  18. function*foo() {
  19. // ...
  20. }
  21. // bad
  22. function *foo() {
  23. // ...
  24. }
  25. // very bad
  26. function
  27. *
  28. foo() {
  29. // ...
  30. }
  31. // very bad
  32. const wat = function
  33. *
  34. () {
  35. // ...
  36. };
  37. // good
  38. function* foo() {
  39. // ...
  40. }
  41. // good
  42. const foo = function* () {
  43. // ...
  44. };

性能

  • 12.1 访问属性时使用点符号。

    const luke = {
    jedi: true,
    age: 28,
    };
    // bad
    const isJedi = luke[‘jedi’];
    // good
    const isJedi = luke.jedi;

  • 12.2 用变量访问属性时使用[]

    const luke = {
    jedi: true,
    age: 28,
    };
    function getProp(prop) {
    return luke[prop];
    }
    const isJedi = getProp(‘jedi’);

  • 12.3 计算指数时使用指数运算符**

    // bad
    const binary = Math.pow(2, 10);
    // good
    const binary = 2 ** 10;

变量

  • 13.1 使用constlet来声明变量。不这样做会导致全局变量,我们要尽可能避免全局污染。

    // bad
    superPower = new SuperPower();
    // good
    const superPower = new SuperPower();

  • 13.2 对每个变量的声明使用单独的constlet

通过这种方式添加新的变量声明更容易,而且您永远不必担心替换掉一个;,或者引入只使用标点的困难。您还可以使用调试器单步执行每个声明,而不是一次跳过所有声明。

  1. // bad
  2. const items = getItems(),
  3. goSportsTeam = true,
  4. dragonball = 'z';
  5. // bad
  6. // (compare to above, and try to spot the mistake)
  7. const items = getItems(),
  8. goSportsTeam = true;
  9. dragonball = 'z';
  10. // good
  11. const items = getItems();
  12. const goSportsTeam = true;
  13. const dragonball = 'z';
  • 13.3 将所有的constlet放在一起。

当以后您可能需要根据前面分配的变量分配一个变量时,这是很有帮助的。

  1. // bad
  2. let i, len, dragonball,
  3. items = getItems(),
  4. goSportsTeam = true;
  5. // bad
  6. let i;
  7. const items = getItems();
  8. let dragonball;
  9. const goSportsTeam = true;
  10. let len;
  11. // good
  12. const goSportsTeam = true;
  13. const items = getItems();
  14. let dragonball;
  15. let i;
  16. let length;
  • 13.4 在需要的时候分配变量,但需要放在合理的位置。

letconst是块级作用域并非函数作用域。

  1. // bad - unnecessary function call
  2. function checkName(hasName) {
  3. const name = getName();
  4. if (hasName === 'test') {
  5. return false;
  6. }
  7. if (name === 'test') {
  8. this.setName('');
  9. return false;
  10. }
  11. return name;
  12. }
  13. // good
  14. function checkName(hasName) {
  15. if (hasName === 'test') {
  16. return false;
  17. }
  18. const name = getName();
  19. if (name === 'test') {
  20. this.setName('');
  21. return false;
  22. }
  23. return name;
  24. }
  • 13.5 不要使用链式变量赋值。

链式变量赋值会创建隐式全局变量。

  1. // bad
  2. (function example() {
  3. // JavaScript interprets this as
  4. // let a = ( b = ( c = 1 ) );
  5. // The let keyword only applies to variable a; variables b and c become
  6. // global variables.
  7. let a = b = c = 1;
  8. }());
  9. console.log(a); // throws ReferenceError
  10. console.log(b); // 1
  11. console.log(c); // 1
  12. // good
  13. (function example() {
  14. let a = 1;
  15. let b = a;
  16. let c = a;
  17. }());
  18. console.log(a); // throws ReferenceError
  19. console.log(b); // throws ReferenceError
  20. console.log(c); // throws ReferenceError
  • 13.6 避免使用一元递增和递减(++--)。

根据eslint文档,一元递增和递减语句受自动分号插入的约束,在应用程序中递增或递减的值可能导致静默错误。使用num+ = 1而不是 num++num++这样的语句来更改值也更有表现力。不允许使用一元递增和递减语句还可以防止您无意中对值进行预递增/预递减,这也会导致程序中出现意料之外的行为。

  1. // bad
  2. const array = [1, 2, 3];
  3. let num = 1;
  4. num++;
  5. --num;
  6. let sum = 0;
  7. let truthyCount = 0;
  8. for (let i = 0; i < array.length; i++) {
  9. let value = array[i];
  10. sum += value;
  11. if (value) {
  12. truthyCount++;
  13. }
  14. }
  15. // good
  16. const array = [1, 2, 3];
  17. let num = 1;
  18. num += 1;
  19. num -= 1;
  20. const sum = array.reduce((a, b) => a + b, 0);
  21. const truthyCount = array.filter(Boolean).length;
  • 13.7 在赋值=之前或之后避免换行符。如果你的赋值违反了max-len,就用()把它括起来。

围绕=的换行符会混淆赋值的值。

  1. // bad
  2. const foo =
  3. superLongLongLongLongLongLongLongLongFunctionName();
  4. // bad
  5. const foo
  6. = 'superLongLongLongLongLongLongLongLongString';
  7. // good
  8. const foo = (
  9. superLongLongLongLongLongLongLongLongFunctionName()
  10. );
  11. // good
  12. const foo = 'superLongLongLongLongLongLongLongLongString';
  • 13.8 不允许声明未使用的变量。

声明了但在代码中没有使用的变量很可能是由于未完成重构而导致的错误。这样的变量会占用代码中的空间,并可能导致读者的混淆。

  1. // bad
  2. var some_unused_var = 42;
  3. // 只写的变量不视为已使用。
  4. var y = 10;
  5. y = 5;
  6. // 修改自身的操作不视为已使用
  7. var z = 0;
  8. z = z + 1;
  9. // 未使用的函数参数
  10. function getX(x, y) {
  11. return x;
  12. }
  13. // good
  14. function getXPlusY(x, y) {
  15. return x + y;
  16. }
  17. var x = 1;
  18. var y = a + 2;
  19. alert(getXPlusY(x, y));
  20. // 'type'未使用会被忽略,因为它有一个默认属性值。
  21. // 这是一种提取\删除对象中指定键的形式。
  22. var { type, ...coords } = data;
  23. // 'coords'现在是没有type属性的data

提升

  • 14.1 var声明被提升到其最接近的封闭函数范围的顶部,它们的赋值则不会。const和let声明有了一个新的概念,称为‘暂时性死区’(TDZ)。这意味着typeof不再是一个百分之百安全的操作。

    // 我们知道这样是行不通的,没有定义全局变量
    function example() {
    console.log(notDefined); // => throws a ReferenceError
    }
    // 在变量声明之前获取变量是可行的,由于变量提升的原因。但赋值并没有提升
    function example() {
    console.log(declaredButNotAssigned); // => undefined
    var declaredButNotAssigned = true;
    }
    //
    function example() {
    let declaredButNotAssigned;
    console.log(declaredButNotAssigned); // => undefined
    declaredButNotAssigned = true;
    }
    //
    function example() {
    console.log(declaredButNotAssigned); // => throws a ReferenceError
    console.log(typeof declaredButNotAssigned); // => throws a ReferenceError
    const declaredButNotAssigned = true;
    }

  • 14.2 匿名函数可以给变量赋值,但不能使用函数声明。

    function example() {
    console.log(anonymous); // => undefined
    anonymous(); // => TypeError anonymous is not a function
    var anonymous = function () {
    console.log(‘anonymous function expression’);
    };
    }

  • 14.3 指定的函数表达式使用变量名,而不是函数体的函数名。

    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’);
    };
    }

  • 14.4 函数声明会提升函数名和函数体。

    function example() {
    superPower(); // => Flying
    function superPower() {
    console.log(‘Flying’);
    }
    }

比较运算符和等式

  • 15.1 使用===!==代替==!=
  • 15.2 如果使用条件语句,例如if语句来抽象判断表达式的值,必须遵循以下尽量简单的原则:

    • Object的值为true
    • Undefined值为false
    • Null值为false
    • Booleans值为布尔值
    • 数字+0,-0,和NaN值为false,其他值为true
    • 字符串''值为false,其他值为true

    if ([0] && []) {
    // true
    // 空数组也是对象 对象值为true
    }

  • 15.3 对布尔值可以使用快捷方式,但对字符串和数字使用显式比较。

    // bad
    if (isValid === true) {
    // …
    }
    // good
    if (isValid) {
    // …
    }
    // bad
    if (name) {
    // …
    }
    // good
    if (name !== ‘’) {
    // …
    }
    // bad
    if (collection.length) {
    // …
    }
    // good
    if (collection.length > 0) {
    // …
    }

  • 15.4 更多信息请参考《Truth Equality and JavaScript》。
  • 15.5 使用大括号创建包含词法声明的case和default子句(例如,let、const、function和class)。

为什么?声明语法在整个switch块中都是可见的,但只有在分配时才初始化,只有在满足条件时才会发生。当多个case子句试图定义相同的内容时,这会导致问题。

  1. // bad
  2. switch (foo) {
  3. case 1:
  4. let x = 1;
  5. break;
  6. case 2:
  7. const y = 2;
  8. break;
  9. case 3:
  10. function f() {
  11. // ...
  12. }
  13. break;
  14. default:
  15. class C {}
  16. }
  17. // good
  18. switch (foo) {
  19. case 1: {
  20. let x = 1;
  21. break;
  22. }
  23. case 2: {
  24. const y = 2;
  25. break;
  26. }
  27. case 3: {
  28. function f() {
  29. // ...
  30. }
  31. break;
  32. }
  33. case 4:
  34. bar();
  35. break;
  36. default: {
  37. class C {}
  38. }
  39. }
  • 15.6 三目运算符不应该嵌套,通常是单行表达式。

    // bad
    const foo = maybe1 > maybe2
    ? “bar”
    : value1 > value2 ? “baz” : null;
    // split into 2 separated ternary expressions
    const maybeNull = value1 > value2 ? ‘baz’ : null;
    // better
    const foo = maybe1 > maybe2
    ? ‘bar’
    : maybeNull;
    // best
    const foo = maybe1 > maybe2 ? ‘bar’ : maybeNull;

  • 15.7 避免不必要的三目运算。

    // bad
    const foo = a ? a : b;
    const bar = c ? true : false;
    const baz = c ? false : true;
    // good
    const foo = a || b;
    const bar = !!c;
    const baz = !c;

  • 15.8 多个运算符混合使用用小括号将其包裹。唯一的例外是标准算术运算符:+-**,因为它们的优先级得到了广泛的理解。建议将/*放在括号中,因为当它们混合在一起时,它们的优先级可能是不明确的。

能够提高可读性和清楚的表达开发人员的意图。

  1. // bad
  2. const foo = a && b < 0 || c > 0 || d + 1 === 0;
  3. // bad
  4. const bar = a ** b - 5 % d;
  5. // bad
  6. // one may be confused into thinking (a || b) && c
  7. if (a || b && c) {
  8. return d;
  9. }
  10. // bad
  11. const bar = a + b / c * d;
  12. // good
  13. const foo = (a && b < 0) || c > 0 || (d + 1 === 0);
  14. // good
  15. const bar = a ** b - (5 % d);
  16. // good
  17. if (a || (b && c)) {
  18. return d;
  19. }
  20. // good
  21. const bar = a + (b / c) * d;

代码块级

  • 16.1 对多行代码块用大括号包裹。

    // bad
    if (test)
    return false;
    // good
    if (test) return false;
    // good
    if (test) {
    return false;
    }
    // bad
    function foo() { return false; }
    // good
    function bar() {
    return false;
    }

  • 16.2 如果您使用带有ifelse的多行代码块,请将else放在与if块的右括号相同的行上。

    // bad
    if (test) {
    thing1();
    thing2();
    }
    else {
    thing3();
    }
    // good
    if (test) {
    thing1();
    thing2();
    } else {
    thing3();
    }

  • 16.3 如果一个if块总是执行一个return语句,那么后面的else块就没有必要了。在包含返回的if块之后的else if块中的返回可以分成多个if块。

    // bad
    function foo() {
    if (x) {
    return x;
    } else {
    return y;
    }
    }
    // bad
    function cats() {
    if (x) {
    return x;
    } else if (y) {
    return y;
    }
    }
    // bad
    function dogs() {
    if (x) {
    return x;
    } else {
    if (y) {
    return y;
    }
    }
    }
    // good
    function foo() {
    if (x) {
    return x;
    }
    return y;
    }
    // good
    function cats() {
    if (x) {
    return x;
    }
    if (y) {
    return y;
    }
    }
    // good
    function dogs(x) {
    if (x) {
    if (z) {
    return y;
    }
    } else {
    return z;
    }
    }

控制语句

  • 17.1 如果控制语句(if, while等)太长或超过了最大行长度,每个(分组的)条件可以放入一个新行。逻辑运算符应该以行开头。

要求操作符位于行首可以保持操作符对齐,并遵循类似于方法链接的模式。这也提高了可读性,使它更容易直观地遵循复杂的逻辑。

  1. // bad
  2. if ((foo === 123 || bar === 'abc') && doesItLookGoodWhenItBecomesThatLong() && isThisReallyHappening()) {
  3. thing1();
  4. }
  5. // bad
  6. if (foo === 123 &&
  7. bar === 'abc') {
  8. thing1();
  9. }
  10. // bad
  11. if (foo === 123
  12. && bar === 'abc') {
  13. thing1();
  14. }
  15. // bad
  16. if (
  17. foo === 123 &&
  18. bar === 'abc'
  19. ) {
  20. thing1();
  21. }
  22. // good
  23. if (
  24. foo === 123
  25. && bar === 'abc'
  26. ) {
  27. thing1();
  28. }
  29. // good
  30. if (
  31. (foo === 123 || bar === 'abc')
  32. && doesItLookGoodWhenItBecomesThatLong()
  33. && isThisReallyHappening()
  34. ) {
  35. thing1();
  36. }
  37. // good
  38. if (foo === 123 && bar === 'abc') {
  39. thing1();
  40. }
  • 17.2 不要使用选择操作符来代替控制语句。

    // bad
    !isRunning && startRunning();
    // good
    if (!isRunning) {
    startRunning();
    }

注释

  • 18.1 使用/ * *……*/用于多行注释。

    // bad
    // make() returns a new element
    // based on the passed in tag name
    //
    // @param {String} tag
    // @return {Element} element
    function make(tag) {
    // …
    return element;
    }
    // good
    /**

    • make() returns a new element
    • based on the passed-in tag name
      */
      function make(tag) {
      // …
      return element;
      }
  • 18.2 对于单行注释使用//。将单行注释放在注释主题上方的新行上。在注释前放置空行,除非它位于块的第一行。

    // bad
    const active = true; // is current tab
    // good
    // is current tab
    const active = true;
    // bad
    function getType() {
    console.log(‘fetching type…’);
    // set the default type to ‘no type’
    const type = this.type || ‘no type’;
    return type;
    }
    // good
    function getType() {
    console.log(‘fetching type…’);
    // set the default type to ‘no type’
    const type = this.type || ‘no type’;
    return type;
    }
    // also good
    function getType() {
    // set the default type to ‘no type’
    const type = this.type || ‘no type’;
    return type;
    }

  • 18.3 以一个空格开始所有的注释,使它更容易阅读。

    // bad
    //is current tab
    const active = true;

    // good
    // is current tab
    const active = true;

    // bad
    /**
    *make() returns a new element
    *based on the passed-in tag name
    */
    function make(tag) {

    1. // ...
    2. return element;

    }

    // good
    /**

    • make() returns a new element
    • based on the passed-in tag name
      */
      function make(tag) {

      // …

      return element;

    }

  • 18.4 在注释前面加上FIXMETODO可以帮助其他开发人员快速理解您是否指出了需要重新检查的问题,或者您是否建议了需要实现的问题的解决方案。这些不同于常规的评论,因为它们是可操作的。这些动作是FIXME: -- need to figure this out或者TODO: -- need to implement
  • 18.5 使用// FIXME:标注问题。

    class Calculator extends Abacus {
    constructor() {
    super();

    1. // FIXME: shouldn’t use a global here
    2. total = 0;
    3. }

    }

  • 18.6 使用// TODO:标注问题的解决方案。

    class Calculator extends Abacus {
    constructor() {
    super();

    1. // TODO: total should be configurable by an options param
    2. this.total = 0;
    3. }

    }

空格

  • 19.1 使用软制表符(空格字符)设置为2个空格。

    // bad
    function foo() {
    ∙∙∙∙let name;
    }

    // bad
    function bar() {
    ∙let name;
    }

    // good
    function baz() {
    ∙∙let name;
    }

  • 19.2 在大括号前放置1个空间。

    // bad
    function test(){
    console.log(‘test’);
    }

    // good
    function test() {
    console.log(‘test’);
    }

    // bad
    dog.set(‘attr’,{
    age: ‘1 year’,
    breed: ‘Bernese Mountain Dog’,
    });

    // good
    dog.set(‘attr’, {
    age: ‘1 year’,
    breed: ‘Bernese Mountain Dog’,
    });

  • 19.3 在控制语句的左括号前放置一个空格(if, while等)。在函数调用和声明中,参数列表和函数名之间不应有空格。

    // bad
    if(isJedi) {
    fight ();
    }

    // good
    if (isJedi) {
    fight();
    }

    // bad
    function fight () {
    console.log (‘Swooosh!’);
    }

    // good
    function fight() {
    console.log(‘Swooosh!’);
    }

  • 19.4 用空格分隔操作符。

    // bad
    const x=y+5;

    // good
    const x = y + 5;

  • 19.5 用一个换行符结束文件。

    // bad
    import { es6 } from ‘./AirbnbStyleGuide’;
    // …
    export default es6;
    // bad
    import { es6 } from ‘./AirbnbStyleGuide’;
    // …
    export default es6;↵

    // good
    import { es6 } from ‘./AirbnbStyleGuide’;
    // …
    export default es6;↵

  • 19.6 在制作长方法链时使用缩进(超过2个方法链)。使用一个前导点,它强调该行是一个方法调用,而不是一个新语句。

    // bad
    $(’#items’).find(’.selected’).highlight().end().find(’.open’).updateCount();

    // bad
    $(’#items’).
    find(’.selected’).
    highlight().
    end().
    find(’.open’).
    updateCount();

    // good
    $(’#items’)
    .find(’.selected’)
    .highlight()
    .end()
    .find(’.open’)
    .updateCount();

    // bad
    const leds = stage.selectAll(’.led’).data(data).enter().append(‘svg:svg’).classed(‘led’, true)
    .attr(‘width’, (radius + margin) * 2).append(‘svg:g’)
    .attr(‘transform’, translate(${radius + margin},${radius + margin}))
    .call(tron.led);

    // good
    const leds = stage.selectAll(’.led’)
    .data(data)
    .enter().append(‘svg:svg’)
    .classed(‘led’, true)
    .attr(‘width’, (radius + margin) * 2)
    .append(‘svg:g’)
    .attr(‘transform’, translate(${radius + margin},${radius + margin}))
    .call(tron.led);

    // good
    const leds = stage.selectAll(’.led’).data(data);

  • 19.7 在块之后和下一个语句之前留下空行。

    // bad
    if (foo) {
    return bar;
    }
    return baz;

    // good
    if (foo) {
    return bar;
    }

    return baz;

    // bad
    const obj = {
    foo() {
    },
    bar() {
    },
    };
    return obj;

    // good
    const obj = {
    foo() {
    },

    1. bar() {
    2. },

    };

    return obj;

    // bad
    const arr = [
    function foo() {
    },
    function bar() {
    },
    ];
    return arr;

    // good
    const arr = [
    function foo() {
    },

    1. function bar() {
    2. },

    ];

    return arr;

  • 19.8 不要用空行填充块。

    // bad
    function bar() {

    1. console.log(foo);

    }

    // bad
    if (baz) {

    1. console.log(qux);

    } else {
    console.log(foo);

    }

    // bad
    class Foo {

    1. constructor(bar) {
    2. this.bar = bar;
    3. }

    }

    // good
    function bar() {
    console.log(foo);
    }

    // good
    if (baz) {
    console.log(qux);
    } else {
    console.log(foo);
    }

  • 19.9 不要使用多个空行来填充代码。

    // bad
    class Person {
    constructor(fullName, email, birthday) {
    this.fullName = fullName;

    1. this.email = email;
  1. this.setAge(birthday);
  2. }
  3. setAge(birthday) {
  4. const today = new Date();
  5. const age = this.getAge(today, birthday);
  6. this.age = age;
  7. }
  8. getAge(today, birthday) {
  9. // ..
  10. }
  11. \}
  12. // good
  13. class Person \{
  14. constructor(fullName, email, birthday) \{
  15. this.fullName = fullName;
  16. this.email = email;
  17. this.setAge(birthday);
  18. \}
  19. setAge(birthday) {
  20. const today = new Date();
  21. const age = getAge(today, birthday);
  22. this.age = age;
  23. }
  24. getAge(today, birthday) {
  25. // ..
  26. }
  27. \}
  • 19.10 不要在小括号内添加空格。

    // bad
    function bar( foo ) {
    return foo;
    }

    // good
    function bar(foo) {
    return foo;
    }

    // bad
    if ( foo ) {
    console.log(foo);
    }

    // good
    if (foo) {
    console.log(foo);
    }

  • 19.11 不要在中括号内添加空格。

    // bad
    const foo = [ 1, 2, 3 ];
    console.log(foo[ 0 ]);

    // good
    const foo = [1, 2, 3];
    console.log(foo[0]);

  • 19.12 在花括号内添加空格。

    // bad
    const foo = {clark: ‘kent’};

    // good
    const foo = { clark: ‘kent’ };

  • 19.13 避免代码行超过100个字符(包括空格)。注意:以上所述,长字符串不受此规则约束,不应被拆分。

确保代码的可读性和可维护性。

  1. // bad
  2. const foo = jsonData && jsonData.foo && jsonData.foo.bar && jsonData.foo.bar.baz && jsonData.foo.bar.baz.quux && jsonData.foo.bar.baz.quux.xyzzy;
  3. // bad
  4. $.ajax({ method: 'POST', url: 'https://airbnb.com/', data: { name: 'John' } }).done(() => console.log('Congratulations!')).fail(() => console.log('You have failed this city.'));
  5. // good
  6. const foo = jsonData
  7. && jsonData.foo
  8. && jsonData.foo.bar
  9. && jsonData.foo.bar.baz
  10. && jsonData.foo.bar.baz.quux
  11. && jsonData.foo.bar.baz.quux.xyzzy;
  12. // good
  13. $.ajax({
  14. method: 'POST',
  15. url: 'https://airbnb.com/',
  16. data: { name: 'John' },
  17. })
  18. .done(() => console.log('Congratulations!'))
  19. .fail(() => console.log('You have failed this city.'));
  • 19.14 要求打开的块与同一行上的下一个括号之间的间距一致。此规则还强制在关闭括号和前一个括号内的同一行上保持一致的间距。

    // bad
    function foo() {return true;}
    if (foo) { bar = 0;}

    // good
    function foo() { return true; }
    if (foo) { bar = 0; }

  • 19.15 不要在逗号前加空格,而要在逗号后加空格。

    // bad
    var foo = 1,bar = 2;
    var arr = [1 , 2];

    // good
    var foo = 1, bar = 2;
    var arr = [1, 2];

  • 19.16 强制计算属性方括号内的间距。

    // bad
    obj[foo ]
    obj[ ‘foo’]
    var x = {[ b ]: a}
    obj[foo[ bar ]]

    // good
    obj[foo]
    obj[‘foo’]
    var x = { [b]: a }
    obj[foo[bar]]

  • 19.17 避免在函数及其调用之间使用空格。

    // bad
    func ();

    func
    ();

    // good
    func();

  • 19.18 在对象属性中强制键和值之间的间距。

    // bad
    var obj = { “foo” : 42 };
    var obj2 = { “foo”:42 };

    // good
    var obj = { “foo”: 42 };

  • 19.19 避免在行的末尾使用尾随空格。
  • 19.20 避免多个空行,只允许在文件末尾有一个换行符,并避免在文件开头有一个换行符。

    // bad - multiple empty lines
    var x = 1;

    var y = 2;

    // bad - 2+ newlines at end of file
    var x = 1;
    var y = 2;

    // bad - 1+ newline(s) at beginning of file

    var x = 1;
    var y = 2;

    // good
    var x = 1;
    var y = 2;

逗号

  • 20.1 不允许逗号在行首。

    // bad
    const story = [
    once
    , upon
    , aTime
    ];

    // good
    const story = [
    once,
    upon,
    aTime,
    ];

    // bad
    const hero = {
    firstName: ‘Ada’
    , lastName: ‘Lovelace’
    , birthYear: 1815
    , superPower: ‘computers’
    };

    // good
    const hero = {
    firstName: ‘Ada’,
    lastName: ‘Lovelace’,
    birthYear: 1815,
    superPower: ‘computers’,
    };

  • 20.2 行尾可以附加逗号。

会导致更干净的git差异。而且,像Babel这样的转置器将在转置代码中删除额外的尾随逗号,这意味着您不必担心遗留浏览器中的尾随逗号问题。

  1. // bad - git diff without trailing comma
  2. const hero = {
  3. firstName: 'Florence',
  4. - lastName: 'Nightingale'
  5. + lastName: 'Nightingale',
  6. + inventorOf: ['coxcomb chart', 'modern nursing']
  7. };
  8. // good - git diff with trailing comma
  9. const hero = {
  10. firstName: 'Florence',
  11. lastName: 'Nightingale',
  12. + inventorOf: ['coxcomb chart', 'modern nursing'],
  13. };
  14. // bad
  15. const hero = {
  16. firstName: 'Dana',
  17. lastName: 'Scully'
  18. };
  19. const heroes = [
  20. 'Batman',
  21. 'Superman'
  22. ];
  23. // good
  24. const hero = {
  25. firstName: 'Dana',
  26. lastName: 'Scully',
  27. };
  28. const heroes = [
  29. 'Batman',
  30. 'Superman',
  31. ];
  32. // bad
  33. function createHero(
  34. firstName,
  35. lastName,
  36. inventorOf
  37. ) {
  38. // does nothing
  39. }
  40. // good
  41. function createHero(
  42. firstName,
  43. lastName,
  44. inventorOf,
  45. ) {
  46. // does nothing
  47. }
  48. // good (note that a comma must not appear after a "rest" element)
  49. function createHero(
  50. firstName,
  51. lastName,
  52. inventorOf,
  53. ...heroArgs
  54. ) {
  55. // does nothing
  56. }
  57. // bad
  58. createHero(
  59. firstName,
  60. lastName,
  61. inventorOf
  62. );
  63. // good
  64. createHero(
  65. firstName,
  66. lastName,
  67. inventorOf,
  68. );
  69. // good (note that a comma must not appear after a "rest" element)
  70. createHero(
  71. firstName,
  72. lastName,
  73. inventorOf,
  74. ...heroArgs
  75. );

分号

  • 21.1 行尾加分号。

为什么?当JavaScript遇到换行符没有分号,它使用的一组规则称为自动分号来确定是否应将换行符结束的一份声明中,和(顾名思义)分号之前到你的代码换行是否这么认为。ASI包含一些古怪的行为,但是,如果JavaScript错误地解释了您的换行符,您的代码将会中断。随着新特性成为JavaScript的一部分,这些规则将变得更加复杂。显式地结束您的语句并加分号结尾。

  1. // bad - raises exception
  2. const luke = {}
  3. const leia = {}
  4. [luke, leia].forEach((jedi) => jedi.father = 'vader')
  5. // bad - raises exception
  6. const reaction = "No! That’s impossible!"
  7. (async function meanwhileOnTheFalcon() {
  8. // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
  9. // ...
  10. }())
  11. // bad - returns `undefined` instead of the value on the next line - always happens when `return` is on a line by itself because of ASI!
  12. function foo() {
  13. return
  14. 'search your feelings, you know it to be foo'
  15. }
  16. // good
  17. const luke = {};
  18. const leia = {};
  19. [luke, leia].forEach((jedi) => {
  20. jedi.father = 'vader';
  21. });
  22. // good
  23. const reaction = "No! That’s impossible!";
  24. (async function meanwhileOnTheFalcon() {
  25. // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
  26. // ...
  27. }());
  28. // good
  29. function foo() {
  30. return 'search your feelings, you know it to be foo';
  31. }

类型分配和强制

* 22.1 在语句的开头执行类型强制转换。
* 22.2 字符串转换。

  1. // => this.reviewScore = 9;
  2. // bad
  3. const totalScore = new String(this.reviewScore); // typeof totalScore is "object" not "string"
  4. // bad
  5. const totalScore = this.reviewScore + ''; // invokes this.reviewScore.valueOf()
  6. // bad
  7. const totalScore = this.reviewScore.toString(); // isn’t guaranteed to return a string
  8. // good
  9. const totalScore = String(this.reviewScore);
  • 22.3 数字:使用Number进行类型转换,parseInt始终使用基数来解析字符串。

    const inputValue = ‘4’;

    // bad
    const val = new Number(inputValue);

    // bad
    const val = +inputValue;

    // bad
    const val = inputValue >> 0;

    // bad
    const val = parseInt(inputValue);

    // good
    const val = Number(inputValue);

    // good
    const val = parseInt(inputValue, 10);

  • 22.4 如果不管出于什么原因,你正在做一些疯狂的事情,parseInt是你的瓶颈,需要使用Bitshift的性能原因,留下一个注释解释为什么和你正在做什么。

    // good
    /**

    • parseInt was the reason my code was slow.
    • Bitshifting the String to coerce it to a
    • Number made it a lot faster.
      */
      const val = inputValue >> 0;
  • 22.5 注意:使用bitshift操作时要小心。数字表示为64位的值,但是位移位操作总是返回32位的整数(源)。位移位会导致大于32位的整数值出现意外行为。讨论。最大32位Int是2,147,483,647:

    2147483647 >> 0; // => 2147483647
    2147483648 >> 0; // => -2147483648
    2147483649 >> 0; // => -2147483647

  • 22.6 布尔值。

    const age = 0;

    // bad
    const hasAge = new Boolean(age);

    // good
    const hasAge = Boolean(age);

    // best
    const hasAge = !!age;

命名规范

  • 23.1 避免使用单个字母的名字。描述你的命名。

    // bad
    function q() {
    // …
    }

    // good
    function query() {
    // …
    }

  • 23.2 在命名对象、函数和实例时使用驼峰命名。

    // bad
    const OBJEcttsssss = {};
    const this_is_my_object = {};
    function c() {}

    // good
    const thisIsMyObject = {};
    function thisIsMyFunction() {}

  • 23.3 仅在命名构造函数或类时使用首字母大写。

    // bad
    function user(options) {
    this.name = options.name;
    }

    const bad = new user({
    name: ‘nope’,
    });

    // good
    class User {
    constructor(options) {
    this.name = options.name;
    }
    }

    const good = new User({
    name: ‘yup’,
    });

  • 23.4 不要在首尾使用下划线。

为什么?JavaScript在属性或方法方面没有隐私的概念。尽管前导下划线通常表示“私有”,但实际上,这些属性是完全公共的,因此也是公共API契约的一部分。这种约定可能导致开发人员错误地认为更改不会被视为破坏,或者不需要测试。如果你想要某样东西是“私人的”,它一定不能明显地存在。

  1. // bad
  2. this.__firstName__ = 'Panda';
  3. this.firstName_ = 'Panda';
  4. this._firstName = 'Panda';
  5. // good
  6. this.firstName = 'Panda';
  7. // good, in environments where WeakMaps are available
  8. // see https://kangax.github.io/compat-table/es6/#test-WeakMap
  9. const firstNames = new WeakMap();
  10. firstNames.set(this, 'Panda');
  • 23.5 不要保存对它的引用。使用箭头函数或函数的bind方法。

    // bad
    function foo() {
    const self = this;
    return function () {
    console.log(self);
    };
    }

    // bad
    function foo() {
    const that = this;
    return function () {
    console.log(that);
    };
    }

    // good
    function foo() {
    return () => {
    console.log(this);
    };
    }

  • 23.6 基本文件名应该与默认导出的名称完全匹配。

    // file 1 contents
    class CheckBox {
    // …
    }
    export default CheckBox;

    // file 2 contents
    export default function fortyTwo() { return 42; }

    // file 3 contents
    export default function insideDirectory() {}

    // in some other file
    // bad
    import CheckBox from ‘./checkBox’; // PascalCase import/export, camelCase filename
    import FortyTwo from ‘./FortyTwo’; // PascalCase import/filename, camelCase export
    import InsideDirectory from ‘./InsideDirectory’; // PascalCase import/filename, camelCase export

    // bad
    import CheckBox from ‘./check_box’; // PascalCase import/export, snake_case filename
    import forty_two from ‘./forty_two’; // snake_case import/filename, camelCase export
    import inside_directory from ‘./inside_directory’; // snake_case import, camelCase export
    import index from ‘./inside_directory/index’; // requiring the index file explicitly
    import insideDirectory from ‘./insideDirectory/index’; // requiring the index file explicitly

    // good
    import CheckBox from ‘./CheckBox’; // PascalCase export/import/filename
    import fortyTwo from ‘./fortyTwo’; // camelCase export/import/filename
    import insideDirectory from ‘./insideDirectory’; // camelCase export/import/directory name/implicit “index”
    // ^ supports both insideDirectory.js and insideDirectory/index.js

  • 23.7 在导出默认函数时使用驼峰命名。您的文件名应该与函数名相同。

    function makeStyleGuide() {
    // …
    }

    export default makeStyleGuide;

  • 23.8 在导出构造函数/类/单例/函数库/对象时使用帕斯卡命名法(PascalCase:每一个单词的首字母大写)。

    const AirbnbStyleGuide = {
    es6: {
    },
    };

    export default AirbnbStyleGuide;

* 23.9 首字母缩写和首字母缩写应该都大写或都小写。

名字是为了可读性,而不是为了满足计算机算法。

  1. // bad
  2. import SmsContainer from './containers/SmsContainer';
  3. // bad
  4. const HttpRequests = [
  5. // ...
  6. ];
  7. // good
  8. import SMSContainer from './containers/SMSContainer';
  9. // good
  10. const HTTPRequests = [
  11. // ...
  12. ];
  13. // also good
  14. const httpRequests = [
  15. // ...
  16. ];
  17. // best
  18. import TextMessageContainer from './containers/TextMessageContainer';
  19. // best
  20. const requests = [
  21. // ...
  22. ];
  • 23.10 只有在导出常量(1)、常量(2)是常量(不能重新赋值)、常量(3)是程序员可以信任常量(及其嵌套属性)不会改变的情况下,才可以选择大写常量。

这是一种额外的工具,用于在程序员不确定某个变量是否会更改的情况下提供帮助。UPPERCASE_VARIABLES让程序员知道他们可以相信变量(及其属性)不会改变。

  • 那么所有的const变量呢?-这是不必要的,所以大写不应该用于文件中的常量。但是,它应该用于导出的常量。
  • 导出的对象呢?-导出顶层的大写(例如:EXPORTED_OBJECT.key),并保持所有嵌套属性不变。

    // bad
    const PRIVATE_VARIABLE = ‘should not be unnecessarily uppercased within a file’;

    // bad
    export const THING_TO_BE_CHANGED = ‘should obviously not be uppercased’;

    // bad
    export let REASSIGNABLE_VARIABLE = ‘do not use let with uppercase variables’;

    // —

    // allowed but does not supply semantic value
    export const apiKey = ‘SOMEKEY’;

    // better in most cases
    export const API_KEY = ‘SOMEKEY’;

    // —

    // bad - unnecessarily uppercases key while adding no semantic value
    export const MAPPING = {
    KEY: ‘value’
    };

    // good
    export const MAPPING = {
    key: ‘value’
    };

属性访问

  • 24.1 属性的访问函数不是必需的。
  • 24.2 不要使用JavaScript的getter /setter方法,因为它们会产生意想不到的副作用,而且很难测试、维护和推理。相反,如果您确实使用了存取函数,那么使用getVal()setVal('hello')

    // bad
    class Dragon {
    get age() {
    // …
    }

    1. set age(value) {
    2. // ...
    3. }

    }

    // good
    class Dragon {
    getAge() {
    // …
    }

    1. setAge(value) {
    2. // ...
    3. }

    }

  • 24.3 如果属性/方法是布尔值,可以使用isVal()hasVal()

    // bad
    if (!dragon.age()) {
    return false;
    }

    // good
    if (!dragon.hasAge()) {
    return false;
    }

  • 24.4 可以创建get()set()函数,但要保持一致。

    class Jedi {
    constructor(options = {}) {
    const lightsaber = options.lightsaber || ‘blue’;
    this.set(‘lightsaber’, lightsaber);
    }

    1. set(key, val) {
    2. this[key] = val;
    3. }
    4. get(key) {
    5. return this[key];
    6. }

    }

事件

  • 25.1 当将数据有效负载附加到事件(无论是DOM事件还是更专有的东西,如主干事件)时,传递一个对象文字(也称为“散列”),而不是原始值。这允许后续参与者向事件有效负载添加更多数据,而无需查找和更新事件的每个处理程序。例如,

    // bad
    $(this).trigger(‘listingUpdated’, listing.id);

    // …

    $(this).on(‘listingUpdated’, (e, listingID) => {
    // do something with listingID
    });
    替换为:

    // good
    $(this).trigger(‘listingUpdated’, { listingID: listing.id });

    // …

    $(this).on(‘listingUpdated’, (e, data) => {
    // do something with data.listingID
    });

jQuery

  • 26.1 用$作为jQuery对象变量的前缀。

    // bad
    const sidebar = $(’.sidebar’);

    // good
    const $sidebar = $(’.sidebar’);

    // good
    const $sidebarBtn = $(’.sidebar-btn’);

  • 26.2 缓存jQuery查找。

    // bad
    function setSidebar() {
    $(’.sidebar’).hide();

    1. // ...
    2. $('.sidebar').css({
    3. 'background-color': 'pink',
    4. });

    }

    // good
    function setSidebar() {
    const $sidebar = $(’.sidebar’);
    $sidebar.hide();

    1. // ...
    2. $sidebar.css({
    3. 'background-color': 'pink',
    4. });

    }

  • 26.3 对于DOM查询使用层级$('.sidebar ul')或父级>子级$('.sidebar > ul').jsPerf
  • 26.4 使用find和限定范围的jQuery对象查询。

    // bad
    $(‘ul’, ‘.sidebar’).hide();

    // bad
    $(’.sidebar’).find(‘ul’).hide();

    // good
    $(’.sidebar ul’).hide();

    // good
    $(’.sidebar > ul’).hide();

    // good
    $sidebar.find(‘ul’).hide();

ES5兼容性

  • 27.1 参考Kangax的ES5兼容性表。

ES5(ES 2015+)新特性

  • 28.1 不要使用尚未达到第三阶段的TC39提案。

为什么?这些文件尚未最后定稿,它们可能会改变或全部撤回。我们想使用JavaScript,而提案还不是JavaScript。

标准程序库

标准库包含一些实用程序,它们在功能上被破坏了,但由于遗留问题仍然存在。

  • 29.1 使用Number.isNaN而不是全局的isNaN

全局isNaN将非数字强制为数字,对强制为NaN的任何内容返回true。如果需要此行为,请将其显式化。

  1. // bad
  2. isNaN('1.2'); // false
  3. isNaN('1.2.3'); // true
  4. // good
  5. Number.isNaN('1.2.3'); // false
  6. Number.isNaN(Number('1.2.3')); // true
  • 29.2 使用Number.isFinite而不是全局的isFinite

全局isFinite将非数字强制为数字,对于强制为有限数字的任何对象返回true。如果需要此行为,请将其显式化。

  1. // bad
  2. isFinite('2e3'); // true
  3. // good
  4. Number.isFinite('2e3'); // false
  5. Number.isFinite(parseInt('2e3', 10)); // true

测试

  • 30.1 是

    function foo() {
    return true;
    }

  • 30.2 必须认真:

    • 无论您使用哪种测试框架,您都应该编写测试!
    • 努力编写许多小的纯函数,尽量减少发生突变的地方。
    • 小心静态方式和模拟数据——它们会使您的测试更加脆弱。
    • 我们在Airbnb上主要用mochajesttape有时也用于小的、单独的模块。
    • 100%的测试覆盖率是一个值得争取的好目标,即使实现它并不总是实际的。
    • 当您修复一个bug时,编写一个回归测试。如果不进行回归测试,修复的bug几乎肯定会在将来再次崩溃。

发表评论

表情:
评论列表 (有 0 条评论,179人围观)

还没有评论,来说两句吧...

相关阅读

    相关 web前端命名规范整理

    web前端命名规范整理 ![img][] 一个得到广泛推崇的东西,必然有它的优势所在。web语义化: 1、可以让人一目了然这块是什么鬼,那块是什么鬼,对于项目的维

    相关 代码规范整理

    > 58到家、快狗打车(58速运)、58家政收简历啦~ > 最近想换工作的同学~ 欢迎拿简历砸我~ 加我微信:15501423004 (记得备注 内推 哈~) 代码规范

    相关 前端代码规范

    html规范 1. 符合web2.0标准,语义化html,结构、表现、行为三层分离,兼容性优良,代码简洁明了有序,尽可能的减少服务器负载,快速的解析速度。 2. 正确

    相关 前端CSS规范整理

    一、文件规范 1、文件均归档至约定的目录中。 具体要求通过豆瓣的CSS规范进行讲解: 所有的CSS分为两大类:通用类和业务类。通用的CSS文件,放在如下目录中: ...