accessor-pairs
在对象和类中强制执行 getter 和 setter 对
在 JavaScript 中,一个常见的错误是只使用属性的 setter 来创建对象,但从来没有为它定义相应的 getter。如果没有 getter,您将无法读取该属性,因此它最终不会被使用。
这里有些例子:
// Bad
var o = {
set a(value) {
this.val = value;
}
};
// Good
var o = {
set a(value) {
this.val = value;
},
get a() {
return this.val;
}
};
如果在没有 getter 的情况下定义了 setter,则此规则会发出警告。使用选项 getWithoutSet
,如果你有一个没有 setter 的 getter,它会发出警告。
规则详情
此规则强制执行一种样式,它要求每个定义了 setter 的属性都有一个 getter。
s66QiXUM0bngOOuGPWRFeVklVOXARrv82rwUVeKIKMAgpRcRLNhbhHQjTVaKQNTAOhu8y78/XSCE6mxYBwq+WNm46GHIkYvGdXoSo2hJ1kw663N7uqyG/pYk/EXwk1QDbfNDlmxWW0TE4bnQf/SK5Lm9S82/1rSt9tx+hZSoqpc=
fLJhGmz7ZY10ceI1yT9/4oguKfQ9I0TrCZUh9dPBO28ccaMnspiZotz+F4GfEc2cpKHKnS+tpwnFVowgoQARqofaY2/145JOtTqGJfrJmvyzGsgRpRnbm2cHMVjVAsSokv1C6eznTYomqqS1BuuMDw==
选项
- cKcO+JhEHO2ogX1nCBzg59Roavwh+B2wItyqJ9X4tZannLe4nT2WGBsXaS1N400mLkAkipB6XmdMNvN4y/wwGnc88uKA4EL6bTS0ZJP3u8puCFvd/NnKlkSyLFwwSL9pEvH9AmVgg3zsUtzluh5ZQZNM5TTJNXPszjBv7QOb0TG9q/CbrLvg4dG1rYLDU8qC2OMergQKtuXVkN//6lnPZ4BraskIePpCv6dUx/zmFjDMVKP/MT+2hvI4E1jV2xrqGheFchKytGvDsyWOUdhAVLWZ2k/Vzq5Krm4HibxC2asxCXDZW1UJZz9bmumJeew/bQVwg1/R0f8txqvOSg7aQmJacJWemxxc4RgMF2rvf9Do7nxGPRIttbOi742ppUdTn2B5KDw6fceqetmmAWk6/GK3307FJfPacC7dtUHRr/M/U3zA8SOR0CHZjm7qUBn4aykdWTl7gFSS2+zkINtghw48/0Amns2Korq7Gluwg/lDSlv9hsbL3xaFiWis8jH4e6rJYBRAKMQQuAApLb9vdj0sS/6Tj+V9URmCnss+buNZf2ePBUiuURSaaWtVg/PqR3NMHSM1fIMABA0btCsGNu6Ua/gLv3gShBQqqWkgSQruurrDoONw8cD8g92qsr3zCjr27LBBF82Bv679QY6wnmYLuQ24eyd5vU3SrDg8U1eyHP+iAAmfHvq3kFy6yfUHhW4iQVO3zCeKA9LZntqh3rcmY2iM+ETkC561BnFLq0A=
setWithoutGet
eqHB0w7lE25hskemPRrxECyhL52R4yPQMy7UM2g5iNFX5orvljIqjj1nP8IeD5XJz71irBUkEA90FE4kydPz2IjTkOuCqz7xq8zmpA/PHEw=
/*eslint accessor-pairs: "error"*/
var o = {
set a(value) {
this.val = value;
}
};
var o = {d: 1};
Object.defineProperty(o, 'c', {
set: function(value) {
this.val = value;
}
});
eqHB0w7lE25hskemPRrxECyhL52R4yPQMy7UM2g5iNFX5orvljIqjj1nP8IeD5XJSA+RWYnwUrHXAynmeCEl1BfPYwTcF0b72vDXrOfFZW4=
/*eslint accessor-pairs: "error"*/
var o = {
set a(value) {
this.val = value;
},
get a() {
return this.val;
}
};
var o = {d: 1};
Object.defineProperty(o, 'c', {
set: function(value) {
this.val = value;
},
get: function() {
return this.val;
}
});
getWithoutSet
VJLWV02/EFC6sjcLWBucgVj3ayTPT/dvqOLREOaFVilHMPr2y1yYUcAhzLhJvbSiv+YtNa/N0sG21ApWS8iMUBTQWPp3CYkUq0t5voNqrU8=
/*eslint accessor-pairs: ["error", { "getWithoutSet": true }]*/
var o = {
set a(value) {
this.val = value;
}
};
var o = {
get a() {
return this.val;
}
};
var o = {d: 1};
Object.defineProperty(o, 'c', {
set: function(value) {
this.val = value;
}
});
var o = {d: 1};
Object.defineProperty(o, 'c', {
get: function() {
return this.val;
}
});
VJLWV02/EFC6sjcLWBucgVj3ayTPT/dvqOLREOaFVilHMPr2y1yYUcAhzLhJvbSiSBZBCVNRCY8d05QIE7TVUOZRv090fGenzsG3F+CiVOw=
/*eslint accessor-pairs: ["error", { "getWithoutSet": true }]*/
var o = {
set a(value) {
this.val = value;
},
get a() {
return this.val;
}
};
var o = {d: 1};
Object.defineProperty(o, 'c', {
set: function(value) {
this.val = value;
},
get: function() {
return this.val;
}
});
enforceForClassMembers
TSBN36polvr6xpCui7OPiFkqpHaoUnHyc2b55aerCwIuAsZzYmPf7ASSqCnS1AojjYLkmg7QX5L9+z5XAHa3TWgj2EXhewvDmAvu83rgCkBFlUfyvcywQfFSkOn5eiB1
- WuALmNkww2aHwaj8pkgJyWFX1WDofx7F05F/PThOfAeAo4SHbGPMj4/d3BbJQ+bKckcjySQZatHDPAF9JsmYvdOcrVDV0DB5DXDRL8AP3aYLocBJgeYRPzsffDb+ovYMK7kEEdPTjvmqqO7Pl7ynemGJ90FKbhS0yvJKp9GL4UqEgWsPDgjdoaNEtFK0hz2L3lsvEaLfAmllfSlF/GO8NAauPj8kuBfLwdQGzoAat2YDwlxGUidLi71G5h4yiBsLRWcnByzTE9zO131aOFkCjQ==
VJLWV02/EFC6sjcLWBucgTyt1JwSA2bc4XMzNsbf7c1XkDTtamLBlmKB/Cw71k4HJLTVwfwlixJGtzslWNNt1Dxf25Xg2VX0lCDgVVY4ffAsw7OQW+B36VkjrNnGhdKU
/*eslint accessor-pairs: ["error", { "getWithoutSet": true, "enforceForClassMembers": true }]*/
class Foo {
get a() {
return this.val;
}
}
class Bar {
static get a() {
return this.val;
}
}
const Baz = class {
get a() {
return this.val;
}
static set a(value) {
this.val = value;
}
}
Nto+kHlaJq9wZqfWhtFl5yGyeLh+J9Ku5umx06fuPIUuILxmiZHczomxy86bZecSmtPFUWX+8Y3b5l5Qb7bRQUeCXWD7LNQ0TCt1UMkL+daR3A0LwUt99IXHvok6ZGye
/*eslint accessor-pairs: ["error", { "setWithoutGet": true, "enforceForClassMembers": true }]*/
class Foo {
set a(value) {
this.val = value;
}
}
const Bar = class {
static set a(value) {
this.val = value;
}
}
TSBN36polvr6xpCui7OPiFkqpHaoUnHyc2b55aerCwIuAsZzYmPf7ASSqCnS1AojcNqz74mSHarTgv+wsyQabqiCBKRiQkVBvgReSf8x7ZBLOEjYUxWFY45dr/DSkg0ig2LIT3dvzjIwLYTN9/1ePQ==
VJLWV02/EFC6sjcLWBucgTyt1JwSA2bc4XMzNsbf7c2v9js7qfmU/WWq/Kr0qT5xIsCyFpKVwiMaYS/yEhETu4CfqZWTYXYrbU5lPt0tadLxKzdg6c2BVChgz4wKrknE9mSDfkKwX6Jc5E5j2LUSDASHbJaiK+nqrUj3uD7Irh4=
/*eslint accessor-pairs: ["error", {
"getWithoutSet": true, "setWithoutGet": true, "enforceForClassMembers": false
}]*/
class Foo {
get a() {
return this.val;
}
}
class Bar {
static set a(value) {
this.val = value;
}
}
const Baz = class {
static get a() {
return this.val;
}
}
const Quux = class {
set a(value) {
this.val = value;
}
}
已知限制
66CpRwKdkcFCjkiriI+KMWd6Y0a9UdbhOzHB7IQzQZU26tmGpcTYVJb+m7MMZNhWwoEs1AtZiAz0NxEKbjlDoTS1Nq5bH81Fl7PpzIOcgMNv7/MnAt5KUBM1bphIMwuCTqimEdgd3M3pmwxp3VKbTlOOpHfDEm6F45MAVbHTiowGnsEIFBvgJR+CoT1xPZHG0S8uhWOPXgV9xGq6X5tQV8bWEFy99qECkkKQ/Gob0gFhMc/pxcbVgaNYlI2y4sYL
/*eslint accessor-pairs: "error"*/
var a = 1;
// no warnings
var o = {
get [a++]() {
return this.val;
},
set [a++](value) {
this.val = value;
}
};
VuJjVmHv264vnH/yAgmy4jwtcUM3NO9Lh6Hs0CTeWd5UI55gjKw3CZs99MPVeKS4LU+BKT8yVG7t+vuLwOFjMmx1eyVxOV6y6SAPSp4tFBeMUx2m59lj2MnDfd3j3fwnHMYtnyvPorDcyFF/rlRpQHc2SwuB60sVvnuYWAWSA0PNfrA0BILvjZb+IQnZgHrgPtqOJsgnQizZhC06/itd3tPWskUI42sazhfErlWUiAg=
/*eslint accessor-pairs: "error"*/
// no warnings
var o = {
get a() {
return this.val;
},
a: 1,
set a(value) {
this.val = value;
}
};
h6lT1EAdmFCk9qocZmEZlbmkd+qlMIvLWxLRXwyL7zlj4ZYIiVXF6HH5mgMcw668jmjUZV8DUoAgvIePEARFewl/5k58RFSwoJlTSXQpSgrz91KzENoMvfs7WhslTOgR
KoSalgh+RGN4uILXwHTjdIXeVsuQ0xYoYYw02Nq7JgsBav1J10kg5ybfof/Q3BMyeIC3KcRjJZTF3YPsQfkSRRBfJFPVngNqAtNPWb3Ki/UhuaydRxu4aPo/+BgS4noCApebplxWJ19P1elsl6BIR+BJGKntuktNQVC52AbhJ7FEZHIibdEByrZj7fQHD3te
9/xOMh3VlHNRT7b8mf+EKPEUIkDzYkW6+Q7vWPci7wowmnae9JqHnI2PPsxbZbonyNSAarTy6iYA+wom+ecWDjwXTN2Xm5cg8j0I7kkPaYcuyOvszATiydGS4Zxta1B2V5Hj/DdnnLkgxbgD+6ZfH5apf3pCKxzsCisZT67Td8rxhuu30LAJ7PMeZU2SLGRw
何时不使用
ZTlqdwrb6n5OFp/GdUMsa1l6ELhGVjx/klalyAUFhdEm4viodmg+nkquJwJogMs5okYgBEXvbSVWcbyfaTmdAf4opZbtYl03iDrl6Tbe02xieOSOog9+LSVRB8qgckeN