Javascript

/**
 * 按钮点击事件
 * @param {number} status 活动状态:1未开始 2进行中 3结束
 * @param {string} identity 身份标识:guest游客 admin管理员
 */
const onButtonClick = (status, identity) => {
  if (identity == 'guest') {
    if (status == 1) {
      //do sth
    } else if (status == 2) {
      //do sth
    } else if (status == 3) {
      //do sth
    } else {
      //do sth
    }
  } else if (identity == 'admin') {
    if (status == 1) {
      //do sth
    } else if (status == 2) {
      //do sth
    } else if (status == 3) {
      //do sth
    } else {
      //do sth
    }
  }
};

// 可优化为
const actions = new Map([
  ['guest_1', () => {} /*do sth*/],
  ['guest_2', () => {} /*do sth*/],
  ['guest_3', () => {} /*do sth*/],
  ['admin_1', () => {} /*do sth*/],
  ['admin_2', () => {} /*do sth*/],
  ['admin_3', () => {} /*do sth*/],
  ['default', () => {} /*do sth*/],
]);

/**
 * 按钮点击事件
 * @param {string} identity 身份标识:guest游客 admin管理员
 * @param {number} status 活动状态:1未开始 2进行中 3 结束
 */
const onButtonClick = (identity, status) => {
  let action = actions.get(`${identity}_${status}`) || actions.get('default');
  action.call(this);
};
// 错误:
const yyyymmdstr = moment().format('YYYY/MM/DD');
// 正确:
const currentDate = moment().format('YYYY/MM/DD');
// 错误:
getUserInfo();
getClientData();
getCustomerRecord();

// 正确:
getUser();
// 错误:
const car = {
  carMake: 'Honda',
  carModel: 'Accord',
  carColor: 'Blue',
};

function paintCar(car) {
  car.carColor = 'Red';
}

// 正确:
const car = {
  make: 'Honda',
  model: 'Accord',
  color: 'Blue',
};

function paintCar(car) {
  car.color = 'Red';
}
// 错误:
function doSomeThing(name) {
  const username = name || 'tony';
  // ...
}

// 正确:
function doSomeThing(name = 'tony') {
  // ...
}
// 错误:
function createMenu(title, body, buttonText, cancellable) {
  // ...
}

// 正确:
function createMenu({ title, body, buttonText, cancellable }) {
  // ...
}

createMenu({
  title: 'Foo',
  body: 'Bar',
  buttonText: 'Baz',
  cancellable: true,
});
class Car {
  constructor(make, model, color) {
    this.make = make;
    this.model = model;
    this.color = color;
  }

  setMake(make) {
    this.make = make;
    return this;
  }

  setModel(model) {
    this.model = model;
    return this;
  }

  setColor(color) {
    this.color = color;
    return this;
  }

  save() {
    console.log(this.make, this.model, this.color);
    return this;
  }
}

const car = new Car('Ford', 'F-150', 'red').setColor('pink').save();