ECMAScript 6 编码规约

2018-05-10  本文已影响12人  勇往直前888

定义

ECMAScript 6(以下简称 ES6)是 JavaScript 语言的下一代标准,ES6 的第一个版本,就这样在 2015 年 6 月发布了,正式名称就是《ECMAScript 2015 标准》(简称 ES2015)。2016 年 6 月,小幅修订的《ECMAScript 2016 标准》(简称 ES2016)如期发布,这个版本可以看作是 ES6.1 版,因为两者的差异非常小(只新增了数组实例的 includes 方法和指数运算符),因此,ES6 是一个泛指, 含义是 5.1 版以后的 JavaScript 的下一代标准,涵盖了 ES2015、ES2016 等,而 ES2015 则是正式名称,特指该年发布的正式版本的语言标准。

规则

变量

// Bad
var x = 'y';
var CONFIG = {};

// Good
let x = 'y';
const CONFIG = {};
// Bad
const a = 0;
a = 1;

const a = 0;
a += 1;

const a = 0;
++a;

for (const a in [1, 2, 3]) {
  // `a` is re-defined (not modified) on each loop step.
  console.log(a);
}

for (const a of [1, 2, 3]) {
  // `a` is re-defined (not modified) on each loop step.
  console.log(a);
}
// bad - unnecessary function call
function checkName(hasName) {
  const name = getName();

  if (hasName === 'test') {
    return false;
  }

  if (name === 'test') {
    this.setName('');
    return false;
  }

  return name;
}

// good
function checkName(hasName) {
  if (hasName === 'test') {
    return false;
  }

  const name = getName();

  if (name === 'test') {
    this.setName('');
    return false;
  }

  return name;
}
// Normal
let a = 3;

// Good
const a = 3;
// Bad
const a = { b: 1 };
a = {};

// Bad
let c = { d: 1 };
c.d = 2;

// Good
const e = { f: 1 };
e.f = 2;

字符串

// Normal
var str = 'Hello, ' + name + '!';
var str = 'Time: ' + 12 * 60 * 60 * 1000;

// Good
var str = 'Hello World!';
var str = `Hello, ${name}!`;
var str = `Time: ${12 * 60 * 60 * 1000}`;

对象

// Bad
var foo = { ['a']: 'b' };

// Good
var foo = { a: 'b' };
// Normal
var foo = {
  x: x,
  y: y,
  z: z
};

var foo = {
  a: function() {},
  b: function() {}
};

// Good
var foo = { x, y, z };

var foo = {
  a() {},
  b() {}
};

函数

// Bad
var x = a => (1 ? 2 : 3);
var x = a => (1 ? 2 : 3);
var x = a => (1 ? 2 : 3);
// Normal
let foo = () => 0;

// Good
let foo = () => {
  return 0;
};
let foo = (retv, name) => {
  retv[name] = true;
  return retv;
};
// Normal
a => {};

// Good
(a) => {};
// Normal
foo(function(a) {
  return a;
});
foo(
  function() {
    return this.a;
  }.bind(this)
);

// Good
foo(a => a);
foo(function*() {
  yield;
});
// Bad
function* foo() {
  return 10;
}

// Good
function* foo() {
  yield 5;
  return 10;
}

function foo() {
  return 10;
}
// Normal
function foo() {
  console.log(arguments);
}

// Good
function foo(...args) {
  console.log(args);
}
function foo(action, ...args) {
  action.apply(null, args);
}
// Normal
myFunction.apply(undefined, args);
myFunction.apply(null, args);
obj.myMethod.apply(obj, args);
obj.myMethod.apply(other, args);

myFunction.call(undefined, arg);
myFunction.call(null, arg);
obj.myMethod.call(obj, arg);
obj.myMethod.call(other, arg);

// Good
Reflect.apply(myFunction, undefined, args);
Reflect.apply(myFunction, null, args);
Reflect.apply(obj.myMethod, obj, args);
Reflect.apply(obj.myMethod, other, args);
Reflect.apply(myFunction, undefined, [arg]);
Reflect.apply(myFunction, null, [arg]);
Reflect.apply(obj.myMethod, obj, [arg]);
Reflect.apply(obj.myMethod, other, [arg]);

// Bad
class A {}
A = 0;

A = 0;
class A {}
// Bad
class Foo {
  bar() {}
  bar() {}
}

class Foo {
  bar() {}
  get bar() {}
}

class Foo {
  static bar() {}
  static bar() {}
}

// Good
class Foo {
  bar() {}
  qux() {}
}

class Foo {
  get bar() {}
  set bar(value) {}
}

class Foo {
  static bar() {}
  bar() {}
}
// Bad
class A {
  constructor() {
    super(); // This is a SyntaxError.
  }
}

class A extends B {
  constructor() {} // Would throw a ReferenceError.
}

// Classes which inherits from a non constructor are always problems.
class A extends null {
  constructor() {
    super(); // Would throw a TypeError.
  }
}

class A extends null {
  constructor() {} // Would throw a ReferenceError.
}

// Good
class A {
  constructor() {}
}

class A extends B {
  constructor() {
    super();
  }
}
// Bad
class A extends B {
  constructor() {
    this.a = 0;
    super();
  }
}

class A extends B {
  constructor() {
    this.foo();
    super();
  }
}

class A extends B {
  constructor() {
    super.foo();
    super();
  }
}

class A extends B {
  constructor() {
    super(this.foo());
  }
}

// Good
class A {
  constructor() {
    this.a = 0; // OK, this class doesn't have an `extends` clause.
  }
}

class A extends B {
  constructor() {
    super();
    this.a = 0; // OK, this is after `super()`.
  }
}

class A extends B {
  foo() {
    this.a = 0; // OK. this is not in a constructor.
  }
}
// Bad
class A {
  constructor() {}
}

class A extends B {
  constructor(...args) {
    super(...args);
  }
}

// Good
class A {}

class A {
  constructor() {
    doSomething();
  }
}

class A extends B {
  constructor() {
    super('foo');
  }
}

class A extends B {
  constructor() {
    super();
    doSomething();
  }
}

模块系统

// Bad
import { merge } from 'module';
import something from 'another-module';
import { find } from 'module';

// Good
import { merge, find } from 'module';
import something from 'another-module';
// Bad
import { foo as foo } from "bar";
export { foo as foo };
export { foo as foo } from "bar";
let { foo: foo } = bar;
let { 'foo': foo } = bar;
function foo({ bar: bar }) {}
({ foo: foo }) => {}

// Good
import * as foo from "foo";
import { foo } from "bar";
import { foo as bar } from "baz";

export { foo };
export { foo as bar };
export { foo as bar } from "foo";

let { foo } = bar;
let { foo: bar } = baz;
let { [foo]: foo } = bar;

function foo({ bar }) {}
function foo({ bar: baz }) {}

({ foo }) => {}
({ foo: bar }) => {}
// Normal
const _ = require('lodash');

// Good
import * as _ from 'lodash';

其他

// Bad
var foo = new Symbol('foo');

// Good
var foo = Symbol('foo');
// Normal
var foo = Symbol();

// Good
var foo = Symbol('some description');
// Bad
parseInt('111110111', 2) === 503;
parseInt('767', 8) === 503;
parseInt('1F7', 16) === 255;

// Good
parseInt(1);
parseInt(1, 3);

0b111110111 === 503;
0o767 === 503;
0x1f7 === 503;

a[parseInt](1, 2);

parseInt(foo);
parseInt(foo, 2);
上一篇 下一篇

猜你喜欢

热点阅读