gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:05:51 25 4
gpt4 key购买 nike

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

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

变成了

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

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

最佳答案

您要查找的规则是object-shorthand。来自 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
};

关于javascript - 对象中的 ES6 风格简写函数是否有 ESLint 规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60401680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com