5 个回答

这2个判断可以并成一个

if (
  type === 1 && this.collect_Status === 0 ||
  type === 2 && this.give_Status === 0
) {
  this.setLinkcollect(type);
} else {
  this.dellinkCollect(type);
}

使用枚举变量代替魔法数字

  • 函数命名应遵循统一规范:小驼峰 setLinkCollect
  • if/else优化:switch、三目、map、class

Example:

this[`${1==(this.collect_Status+type)?'set':'del'}LinkCollect`](type)
this[`${2==(this.give_Status+type)?'set':'del'}LinkCollect`](type)

个人会选择点赞和收藏处理程序分开, 内部根据状态三目运算

if ((type === 1 && this.collect_Status === 0) || (type === 2 && this.give_Status === 0)) {
    this.setLinkcollect(type)
  } else {
    this.dellinkCollect(type)
  }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题