【问题标题】:Is there an ESLint rule for ES6 style shorthanded functions in objects?对象中的 ES6 风格的简写函数是否有 ESLint 规则?
【发布时间】:2020-06-09 14:29:36
【问题描述】:

在 ES6 中,当我们的函数是对象中的属性时,我们不再需要使用 function 关键字:

const obj = {
    hello: function() { console.log("world!"); }
};

变成

const obj = {
    hello() { console.log("world!"); }
};

是否有强制使用新样式的 ESLint 规则?我在ES6 rule list 中没有看到类似的内容。

【问题讨论】:

标签: javascript ecmascript-6 eslint


【解决方案1】:

您要查找的规则是object-shorthand。来自the docs:

以下每个属性都会发出警告:

/*eslint object-shorthand: "error"*/
/*eslint-env es6*/

var foo = {
    w: function() {},
    x: function *() {},
    [y]: function() {},
    z: z
};

在这种情况下,预期的语法应该是:

/*eslint object-shorthand: "error"*/
/*eslint-env es6*/

var foo = {
    w() {},
    *x() {},
    [y]() {},
    z
};

【讨论】:

  • 谢谢!浏览文档时,我的大脑正在搜索 function 这个词 - 一定是这样的。
猜你喜欢
  • 2021-08-20
  • 1970-01-01
  • 2019-02-01
  • 2019-10-23
  • 2021-07-16
  • 2021-07-21
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多