object-shorthand
要求或禁止对象字面量的方法和属性简写语法
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
ECMAScript 6 为定义对象字面量方法和属性提供了一种简洁的形式。这种语法可以使定义复杂的对象字面量更加清晰。
以下是一些使用 ES5 语法的常见示例:
// properties
var foo = {
x: x,
y: y,
z: z,
};
// methods
var foo = {
a: function() {},
b: function() {}
};
现在这里是 ES6 等价物:
/*eslint-env es6*/
// properties
var foo = {x, y, z};
// methods
var foo = {
a() {},
b() {}
};
规则详情
sv1h18oMLt12Z34UMGnARCWaNpRFN4t8tBCE1yP+AbgTt+mF8pFz/T0My070F/Ll5hjGTayyu5tuVwzZiwM5NmBnwUSR5MHqc39ud6bToe0wf8JBB5MXLGLnAU1jXNs0RUKCm8oyev9SQ+4weRmnQT0HDwm069no06GTXRoakB3IukGSsbqkNmLtTEwhob/VDfH53p/UyfGIHSwMUaUSCgamZIrA7bBCWaByHOssxQd8TNittHbdM0O6mJUjBbLSsZS+oicZSZDgv9Pjp8Z6hQ==
++yU/GJMdp7tc3BTUBG/bJsUeqNbihdeZvVTnQnFzQ1HOtx3cWApF5kA9ClS9y6/
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
w: function() {},
x: function *() {},
[y]: function() {},
z: z
};
+mXpCdvpi45iWz8PmRGKwX7DPzgoSaSUEZB4XJAfDDAFXcXRFzvuvfoj1ZsxKQOkTLSUn+DsO17zLRCYc/f1lw==
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
w() {},
*x() {},
[y]() {},
z
};
TkAiL9cb8ZqkY5Lgh5lhm9dcfY9+5tdUeLXSY/2vXt0+hH0NpgCaGdGithOq1BNBBHBe7qqXo1BdJjBW+NRn3tnglPBvDgevAMN7XEIEuq8=
/*eslint object-shorthand: "error"*/
/*eslint-env es6*/
var foo = {
x: (y) => y
};
选项
O/XgU19OKjlFjJqXM+T1dQUyZBhZb8YIw5yrGM9GmRhPUpUp+IVEvJCIei1qG84y0qOeUtplZ87m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BswQ7meQ1nKhIsDh8T17zrPnVBiQ7ja5AxrI7NU7aiu9OVYuAk+KRaRlBphoK/fgYoCrwVeGLRl41TywNFJxVEEoSNrWq+t4rvkn36mTc9nYaR1HQiiwCEiBcm/A4gT0jcmDbu3VRtvqifde+jpI07L2hWPd64xG9Ip3Mn+q0TxoOsJ1FyJt5EaiDpPIJAuuLgCs6XyVLxJ6aEa9Fnn1N+y30RyF8zsb3moD8PntPB5S8IBMMDy9tmt3s/5S2R9CiiAEGT7IRnVJla8VyxJiU1TB3NpKQstksBfHA+cI1khUBZzttvkqlepPt0sG9o7p1olZOC3e45dxiQFRyhSzHtjdnl7Ct4n8Vp0lpbnXMG90pvHnei12ve7qR59PQIehGGSucv1w9D327QauZiJBIf7ZwBEzl7HLGEg8qag+cOSkW99ZpzOLYgsXX3N85o1Lf6DY2KuwX68b2S97ROr45diLDvYjOWDAFd1SRNLUsfmNbzmgZjuko=
avoidQuotes
{
"object-shorthand": ["error", "always", { "avoidQuotes": true }]
}
DpWMIyZPwSJTs/PJwc2JhOW7cGqtVf2DrstN763uVxvgpPKUMpI5c+SIQjKE5WcOaoErCfMAkqCN+LoMOVweE6ysONye5REx9HWfB8CkojMYdn1aRoAS+qjGDUPuZ89CLcQoA4ClHzoulgE9nvqroQ==
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
/*eslint-env es6*/
var foo = {
"bar-baz"() {}
};
ACbGoO0F0WET2g6e840uzHPurqs0/15GikeRDjE6M2kkWsoEPGpO74gYEIOqJtutmLY83Keg9fbZdWJrizOEuXV3BJevkTRye1TSEglpWzw2NnzJJg5nx3BWSPgkB3Ko
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
/*eslint-env es6*/
var foo = {
"bar-baz": function() {},
"qux": qux
};
ignoreConstructors
{
"object-shorthand": ["error", "always", { "ignoreConstructors": true }]
}
ACbGoO0F0WET2g6e840uzHPurqs0/15GikeRDjE6M2kMQ9IVUzBlZcT3/7Ecvphv7JPw1kmX2/jxaqTQ9puo4dYnn7p08LeeNSIrLUY5cVJTWlS+A9IxGS0rlOxWXGvJNZLA4zxyypTFeJsCFB7Vdw==
/*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/
/*eslint-env es6*/
var foo = {
ConstructorFunction: function() {}
};
methodsIgnorePattern
ACbGoO0F0WET2g6e840uzHPurqs0/15GikeRDjE6M2noMIR0fOZY5iVhNvmXhoVH5p2Z4k5F6AD/v421SDU96amN13raP8BaEaTzNVJnhDm1JSZZg+tEkuYIX6hB6N5G5/Z7jOAOMUY95yLIAfa0vA==
/*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^bar$" }]*/
var foo = {
bar: function() {}
};
avoidExplicitReturnArrows
{
"object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
}
DpWMIyZPwSJTs/PJwc2JhM2REXuAhUWBUuRxjRtX9T7q4e9t+NdwvAqhJBFVkROk1Hyd5+rJLIuYOMwuIW3yp+Wxr+tE5abfVoYg0tXEm3xSc00CHhWKlqOap6TKQUq6C//TgI1fOZ/xcdsoCtaODQ==
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
/*eslint-env es6*/
var foo = {
foo: (bar, baz) => {
return bar + baz;
},
qux: (foobar) => {
return foobar * 2;
}
};
ACbGoO0F0WET2g6e840uzHPurqs0/15GikeRDjE6M2m2+U97lyXgvqMr2Us4O3+WMeheMp3SaDVpCQdNR+YX/CYEOy2jDZsSci2lxoSeM53JRJjsfEKrmLB0p/bgnbDxW7CxPSBDLzCktWX43QWx0Q==
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
/*eslint-env es6*/
var foo = {
foo(bar, baz) {
return bar + baz;
},
qux: foobar => foobar * 2
};
2WSI5Am6q7eVe3DWIJQ8y5Z88x4h0Msjxt5Awc6UST6XjjTI6WfrDdtWZ/Qtf/L5Ge0x+Fs5Baa4MQ4kJUIfovgOqsqTvqADRrN/um6gzXQ=
/*eslint object-shorthand: [2, "consistent"]*/
/*eslint-env es6*/
var foo = {
a,
b: "foo",
};
ACbGoO0F0WET2g6e840uzAmVkRCG0tbyG2mbz68rfLsynf5GoZTbdIMnzMqja4uPllfp6hEeVGngspqBraT0PXbbCAYiSCAKItp5oBCx1Kg=
/*eslint object-shorthand: [2, "consistent"]*/
/*eslint-env es6*/
var foo = {
a: a,
b: "foo"
};
var bar = {
a,
b,
};
2WSI5Am6q7eVe3DWIJQ8y6FpY9yGZf2KP1xZzvT4/6aWO7q/6s/6bRhPGeHb9/bTRLeF6uiPGicH5QwuwOIIHdFKrihO5EQHBiOzVyaU6h7cwYXHk8gD9Pw1ZZp3KOet4MPSXuDhDOkUZMHKc/g4iijgXp42isqjOdGBwqLMrTs=
/*eslint object-shorthand: [2, "consistent-as-needed"]*/
/*eslint-env es6*/
var foo = {
a: a,
b: b,
};
何时不使用
1fvmfjllrcKcDMYeSA/sHhKqA2O+sriublPA22LfHfv8hrRTw5a9km4ZXKn50Ofs3jBILHy9oTQUvy94MRo59G5qHjJZmbTTLHW8cu+7p1XFtepcFkM0frSlD4+CtnTOkqmtUjGXgebDwDDvYcJUnvMXy96CCoEl/HCz2p/iT9iMNeak7020C40TMSyaS/T4wWEJw3Dwm8UcpDboKZPvFj0bY4S49es5KJ0K79xOkgufNSvSQG2PPlbu6FvD6f3E