简体   繁体   English

对象中的 ES6 风格简写函数是否有 ESLint 规则?

[英]Is there an ESLint rule for ES6 style shorthanded functions in objects?

In ES6 we no longer have to use the function keyword when our function is a property in an object:在 ES6 中,当我们的函数是对象中的属性时,我们不再需要使用function关键字:

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

becomes变成

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

Is there an ESLint rule to enforce using the new style?是否有强制使用新样式的 ESLint 规则? I don't see anything like it in the ES6 rule list .我在ES6 规则列表中没有看到类似的内容。

The rule you're looking for is object-shorthand .您正在寻找的规则是object-shorthand From the docs :文档

Each of the following properties would warn:以下每个属性都会发出警告:

 /*eslint object-shorthand: "error"*/ /*eslint-env es6*/ var foo = { w: function() {}, x: function *() {}, [y]: function() {}, z: z };

In that case the expected syntax would have been:在这种情况下,预期的语法是:

 /*eslint object-shorthand: "error"*/ /*eslint-env es6*/ var foo = { w() {}, *x() {}, [y]() {}, z };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM