不要改变形参的值

  1. onServiceCardAdd({
  2. currentTarget: {
  3. dataset: {
  4. item,
  5. }
  6. }
  7. }) {
  8. const initPrepurchases = this.data.pagePrepurchases ? this.data.pagePrepurchases : this.data.initPrepurchases;
  9. if (item.limitNumber > 0 && item.count <= item.maxCount) {
  10. item.limitNumber -= 1;
  11. item.count += 1;
  12. this.data.num++;
  13. initPrepurchases.map(initItem => {
  14. if (item.id === initItem.id) {
  15. // initItem的值发生了变化,数据源如果被用在了其他地方就有可能导致逻辑错误
  16. initItem.count = item.count;
  17. }
  18. });
  19. this.setData({
  20. num: this.data.num
  21. });
  22. const storeId = this.data.storeInfo.id;
  23. this.requestBookingPrepurchaseMaxCount({storeId, prePurechases: initPrepurchases});
  24. }
  25. }
  1. // 不建议
  2. this.data.skuList.forEach(item => {item['isSelected'] = true; });
  3. // 建议
  4. this.data.skuList = this.data.skuList.map(item => ({
  5. ...item,
  6. isSelected: true,
  7. }));