JS Style Guide_1

  1. 当你在回调函数里要使用函数表达式时,尽量使用箭头函数,比如数组中的 Map、filter、reduce等的回调函数中
[1,2,3].map((x) => {
    let y = x + 1;
    return x * y;
});

  1. 如果函数只有一个参数并且函数体没有大括号,就删除参数的圆括号
[1,2,3].map(x => x * x)
  1. 箭头函数的函数体涉及多行,则把函数体包含在圆括号里更具有可读性
function httpMagicObject() {
    
}
['get', 'post', 'put'].map(httpMethod => (
    Object.prototype.hasOwnProperty.call(
        httpMagicObject,
        httpMethod
    )
))

  1. 多行import因该缩进,像数组和对象字面量那样
import {
  nameA,
  nameB,
  nameC
  ...
} from 'path'
  1. 获取的属性是变量时用方括号[]来获取
const luke = {
  jedi: true,
  age: 28,
};

function getProp(prop) {
  return luke[prop];
}

const isJedi = getProp('jedi');
  1. 使用 +=或者-=来替代自增或者自减运算符

原文地址:https://www.cnblogs.com/yuanchao-blog/p/11395540.html