grouped-accessor-pairs

在对象字面量和类中需要分组访问器对

相同属性的 getter 和 setter 不一定必须彼此相邻定义。

例如,以下语句将创建相同的对象:

var o = {
    get a() {
        return this.val;
    },
    set a(value) {
        this.val = value;
    },
    b: 1
};

var o = {
    get a() {
        return this.val;
    },
    b: 1,
    set a(value) {
        this.val = value;
    }
};

虽然允许在对象或类定义中的任何位置为 getter 或 setter 定义对,但将访问器函数分组为同一属性被认为是最佳实践。

换句话说,如果一个属性有一个 getter 和一个 setter,那么 setter 应该在 getter 之后定义,反之亦然。

规则详情

DfCavJkKwo0qE1ph9kwrWHiEHCT3eSioGAqcZCgV3wGHwQ+2WGwfJfyFvYlNf7BzXHokYY4q/ubiIcLdOmOWSidIZv4+A4Skn5ugIP3o/hGfpotqEcKSe81VuNOPMAWWMgGvgw78Y9SXATQJRiBDYw==

baVYhAibLGkFa5kli7XsXGZQHNjkAiZDjkBuwIEHeq9qDg1MZoMFpQ2g7xxMDkKdL+rRImzDTNwxePKdQ+kvfyYUaV1Ekvfrgd/xTmWbyLD9UfeUMF/6VX+aReJVb4hHt0ptn2ZvZyOro8GBjd/CBjkuivrzfLFwPY0XuDX+nxI=

Ie0TZ5Pypu/OkAjnJK0FCflF+k3cwN8tT5v0DWcn79jogZcNxMmWnfDdvdSQc585fBDyg5DkyusfFVKe8sBLh7yFr/2+gxgo3ohCENzyXOnHnGKGcFLbGtDXp/i/rFNMU+ShI5ZXKAP5jnV11zJxahpnSe967LX7SpV2PNIJU7uODqoAS0VeYQjcpkF5YHdF311UdyrswjstNMS2+Yu08u7C3J5r+cmy9cBDj7x1yb3ldpVxFPbEpAog0n0yM7d5

53qbJC6xNcTpfWzDOxH3FbxQniXPcALMzc0838bz9bxwfaPpKG0ZV7pRGyxgA1aA

/*eslint grouped-accessor-pairs: "error"*/

var foo = {
    get a() {
        return this.val;
    },
    b: 1,
    set a(value) {
        this.val = value;
    }
};

var bar = {
    set b(value) {
        this.val = value;
    },
    a: 1,
    get b() {
        return this.val;
    }
}

class Foo {
    set a(value) {
        this.val = value;
    }
    b(){}
    get a() {
        return this.val;
    }
}

const Bar = class {
    static get a() {
        return this.val;
    }
    b(){}
    static set a(value) {
        this.val = value;
    }
}

x8vS/D2xVZSiiHLs1PwUl6/TVEXV0uticb8dGcMm9rxiwxh8yRqnjwkHP0i+2Pam

/*eslint grouped-accessor-pairs: "error"*/

var foo = {
    get a() {
        return this.val;
    },
    set a(value) {
        this.val = value;
    },
    b: 1
};

var bar = {
    set b(value) {
        this.val = value;
    },
    get b() {
        return this.val;
    },
    a: 1
}

class Foo {
    set a(value) {
        this.val = value;
    }
    get a() {
        return this.val;
    }
    b(){}
}

const Bar = class {
    static get a() {
        return this.val;
    }
    static set a(value) {
        this.val = value;
    }
    b(){}
}

选项

GrNq2fetYiou0eUGLFslKU8WePPxoHXX7hjYdZAziDPNAaxCk4B4Y3hgSZuNA1JV

    o0OWEIRPEnHUzFUnAJwlqn1fFdyAPI3Vit3Zb81Iqfh+MVKEVEWJCCvnFMf2/wGEElEexBeX8OG0H1hyVPQvPVum41wEb/HGlliv9A3V9LrFV4iZVgrm5a5R2m6+exc5NtNDrOObWNHMTReHWPxDR/4izKs1AMTpgnVhAwOU5rFbBVZ1rc4Ab6pizGJxNVJJP5siBdXGIomIzedq+Gx/oVkrrjxPsihBcLIe2JTH+OIwVeoFSugHrvClc4xGA7ONDZAW5CeqGkyRCnHMYcafxzGwvSKnZqioX5E2cogz5Mhc41CEYKOIJQocOJoHAyokFQArycGA9bwIiArfQOCZUBb8p4xKq75AqV/udaL4w2W168fNFmmpgxUwHlYlXV3xGnqFrMl41UhK4mJSxcN1s8fHsdxM49+lkHwVWA2oOlwrOEu0h3VXAC5jBF7aUg96

getBeforeSet

5nEv4RCYCaBiSjFxJ4F/mGLf5JF4w2nOuic6PF7t+/g6mqTc1hJhNXwdq+0KgB0oYplKRJ2wRbfN/VmT/HqO4xXb8hwqI5c4A+FR72vapZQ=

/*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/

var foo = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};

class Foo {
    set a(value) {
        this.val = value;
    }
    get a() {
        return this.val;
    }
}

const Bar = class {
    static set a(value) {
        this.val = value;
    }
    static get a() {
        return this.val;
    }
}

5nEv4RCYCaBiSjFxJ4F/mGLf5JF4w2nOuic6PF7t+/g6mqTc1hJhNXwdq+0KgB0oi48IQPWGuHPwrFYROM1u709tzy4rAl6O/XTLXJRcZH0=

/*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/

var foo = {
    get a() {
        return this.val;
    },
    set a(value) {
        this.val = value;
    }
};

class Foo {
    get a() {
        return this.val;
    }
    set a(value) {
        this.val = value;
    }
}

const Bar = class {
    static get a() {
        return this.val;
    }
    static set a(value) {
        this.val = value;
    }
}

setBeforeGet

5nEv4RCYCaBiSjFxJ4F/mE/OL9wXqHR6TScim/cNiv1VcfFTHhv3hPxccpZwrbbrXO9pKvn31+NF8rfjDWG+C2ye7ulcsQFIGKURKjfUIKs=

/*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/

var foo = {
    get a() {
        return this.val;
    },
    set a(value) {
        this.val = value;
    }
};

class Foo {
    get a() {
        return this.val;
    }
    set a(value) {
        this.val = value;
    }
}

const Bar = class {
    static get a() {
        return this.val;
    }
    static set a(value) {
        this.val = value;
    }
}

5nEv4RCYCaBiSjFxJ4F/mE/OL9wXqHR6TScim/cNiv1VcfFTHhv3hPxccpZwrbbr4H6Ry9QpNTAipu5PnxJL9HdeKhkUG/Z2zUGA3Ze0V5Q=

/*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/

var foo = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};

class Foo {
    set a(value) {
        this.val = value;
    }
    get a() {
        return this.val;
    }
}

const Bar = class {
    static set a(value) {
        this.val = value;
    }
    static get a() {
        return this.val;
    }
}

已知限制

mO++PuoSR+WRyAda++l/ohET11Cx0aO9ZsI5N9iNf/WpH7z7NQldND8xmlckqFGlPKNdkfHV0Zl0TaysbyQ252q5K2n9wR9SqZx80sbg3vsZkFFwIf3cvJEnS1h+kTfZr4qBaTBTcbVeEAigTG5d85hCrfyEzB3YnrXTWNlVEQlj3ocMzpdQ/WsBdUSSwe4J7UU1SkqZi4gnvYZZm1rSpGMPp+zP8JksuxtbIs3L4wVNTGp/AHd8T+3eAf5T3sPF3JHsjXrySaEV1G+b2o+Ilg==

/*eslint grouped-accessor-pairs: "error"*/

var a = 1;

// false warning (false positive)
var foo = {
    get [a++]() {
        return this.val;
    },
    b: 1,
    set [a++](value) {
        this.val = value;
    }
};

// missed warning (false negative)
var bar = {
    get [++a]() {
        return this.val;
    },
    b: 1,
    set [a](value) {
        this.val = value;
    }
};

zsZIaICUqZNPyQpj6fim98ulhrChMvUlh6SdkNmUFrCNGw3YqKp14JVFZa4C2jgDIDEeuETDLhfaKp79myvwi5OwdCvHN24GzaUJJq1I6pAKLbNefIkZleuMRlKRpvPZ

5C1fLotSJYdqcc23+NmR6krUWuBLqdQOV11WbppzxdEnKZOKf8exxtiYOkwGnFs03Csob+DYLyRtcotPZw3Dbtc/mJzwrW6yMMi6cW5L5teN8F7TU9oOBTrSoMrmrSHakM+E7Ivn+3fbSqL/OpyQPYVJElqJGuPmBWSnkSjTiG8c59hme9gRcPS2/WeioWaW

EyZ45cJPMOqklBYif7j5qHOcqQIIZson88YvTauz0X+MQ7HohhXBOHS0VF0WrfhzweTkysHaYm6ngcmO81ihGWG4jTcOdO2Tp7KUPFfVauy88mE+Ux22l9VMvmB4KDLBLfOMqFLGGnjfj82VPd3OVPre+ijmJSC7QxGFW+gd1AZ6T3VV7YDNJ21lZe7TfcuG