使用简单的箭头函数修复 'consistent-return' linter 问题

Fixing 'consistent-return' linter issue with simple arrow function

如标题,我有一个错误的箭头函数。我怎样才能重构它以使错误消失?

Expected to return a value at the end of arrow function consistent-return

这是我的代码:

// Disable default form submission when user hits enter.
const $facetInput = $('.facet-input');
const $submitButton = $('.facet-submit');

$facetInput.on('keydown', (event) => {
  if ((event.key === 'enter' || event.keyCode === 13) && event.target !== $submitButton) {
    event.preventDefault();
    return false;
  }
});

谢谢!

来自 always explicitly returning a value under all code branches:

$facetInput.on('keydown', (event) => {
  if ((event.key === 'enter' || event.keyCode === 13) && event.target !== $submitButton) {
    event.preventDefault();
    return false;
  }
  return <something else>; // false, true, 'whatever' - up to you
});

假设这是 eslint(但实际上与 linter 无关),如果您的 if (condition) 未评估为真,则存在隐式 return undefined

One of the confusing aspects of JavaScript is that any function may or may not return a value at any point in time. When a function exits without any return statement executing, the function returns undefined. Similarly, calling return without specifying any value will cause the function to return undefined. Only when return is called with a value is there a change in the function's return value.