意外的函数表达式.(喜欢箭头回调)

Dar*_*ren 1 javascript reactjs

我在下面的代码中收到三个错误eslint,虽然该函数似乎在应用程序中按预期工作.

错误:

[eslint] Unexpected unnamed function. (func-names)
[eslint] Unexpected function expression. (prefer-arrow-callback)
[eslint] Do not access Object.prototype method 'hasOwnProperty' from target object. (no-prototype-builtins)
Run Code Online (Sandbox Code Playgroud)

码:

const getCount = campaignData.reduce(function(acc, curr) {
  if (!acc.hasOwnProperty(curr.status)) {
    acc[curr.status] = 1
  } else {
    acc[curr.status] += 1
  }
  return acc;    
}, {});
Run Code Online (Sandbox Code Playgroud)

我在renderReactJS组件中使用{getCount.active}.

在它工作的时候,我应该忽略它吗?

Jon*_*lms 7

这就是linter的含义:

[eslint]意外的未命名功能.(FUNC-名)

为函数赋予适当的名称会使错误堆栈更具可读性,error at accumulate更具表现力,error at anonymous因此它建议您更改:

 function(acc, curr)
Run Code Online (Sandbox Code Playgroud)

 function accumulate(acc, curr)
Run Code Online (Sandbox Code Playgroud)

或者,您可以遵循以下建议:

[eslint]意外的函数表达式.(喜欢箭头回调)

如果你不需要this在函数内部,你可以使用箭头函数语法,它会更短.

 function(acc, curr) {
Run Code Online (Sandbox Code Playgroud)

 (acc, curr) => {
Run Code Online (Sandbox Code Playgroud)

[eslint]不要从目标对象访问Object.prototype方法'hasOwnProperty'.(无原型内建)

继承的类可能会意外地覆盖它,因此它建议您使用:

 curr.status in acc
Run Code Online (Sandbox Code Playgroud)

但所有这些提示实际上只是可读性的一些改进,没有功能差异.

在它工作的时候,我应该忽略它吗?

如果您认为"如果代码工作正常",那么是,否则不是.