как может JS, если еще цикл может быть написан в технике Ts Shorttend - PullRequest
2 голосов
/ 09 октября 2019

У меня есть цикл if, который устанавливает isCustomerFunctionSelected как true и false и устанавливает значение как null, как я могу переписать это в технике сокращенного TS.

  this.reset = false;

  if(this.functionValue === 'Customer') {
    this.isCustomerFunctionSelected = true;
  } else if (this.functionValue === 'Dealer') {
    this.isCustomerFunctionSelected = false;
    this.selectedCustomerValue = null;
  } else {
      this.reset = true;
  }

1 Ответ

1 голос
/ 09 октября 2019

Не будет сокращать его, но облегчит его поддержку и развитие.

const {
  func,
} = ([{
  value: 'Customer',

  func: () => {
    this.isCustomerFunctionSelected = true;
  },
}, {
  value: 'Dealer',

  func: () => {
    this.isCustomerFunctionSelected = true;
    this.selectedCustomerValue = null;
  },
}].find(x => x.value === this.functionValue) || {
  func: () => {
    this.reset = true;
  },
});

func();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...