no-param-reassign

不允许重新分配 function 参数

对声明为函数参数的变量赋值可能会产生误导并导致行为混乱,因为修改函数参数也会改变 arguments 对象。通常,分配给函数参数是无意的,并且表示错误或程序员错误。

此规则也可以配置为在修改函数参数时失败。对参数的副作用可能会导致违反直觉的执行流程并使错误难以追踪。

规则详情

此规则旨在防止因修改或重新分配函数参数而导致的意外行为。

此规则的错误代码示例:

/*eslint no-param-reassign: "error"*/

function foo(bar) {
    bar = 13;
}

function foo(bar) {
    bar++;
}

function foo(bar) {
    for (bar in baz) {}
}

function foo(bar) {
    for (bar of baz) {}
}

DzO9VpSFHJiTjGCgt1KSooNhAIn1o64th84QTOygwWSy7fBgj4TGxtI1vaW0J9Ta

/*eslint no-param-reassign: "error"*/

function foo(bar) {
    var baz = bar;
}

选项

6c/jwvvm7k/PNv7EyMbJuUshnvexy9a5fCLR3fDpTXjUU787lhI5jWaIvY+kMV6iFAg+jzCHM/yTE3yp3PUm5H0xCL2Edv1jq+/iNTHWDhM302tBTIYEUAt52fGGgu7rOPWgN4RC3UPmqW8azup/Fl7XFgRi+SaBPekCreC+fanD0e2scjY+xGRk7y7bjJV3yX9gCMCMP+PKc9XkwywZXKVs1qcYU4igrS5r/rDOyktXpr+u7kai520VPRHFKOoIvZ2JiTyMsFkcQ8O1WJgv6c/9ZBVnrukBxLoN8OKbhx2ZCixqNehkFrNdMWgIBB26gJIDDdrd2oD8AeWazMSZxCbIi+agm/rRskbpZ/rl7lZEyj9h/nrrcTSq/84qjQ5X4IUHNeZ3pGiETgbVMWwI2EPRmF2g7wybT1MYwQAf21tVLkRVupk5nr9zjOekhuLb3RuAB+Ko7ba1dZ7yMnO+pfG9r7HGkuMN79Rm0KfKfFrX8ioGxm5j1x08MYrYvSju+nbah+esiiZJQcqebwWTRKnX0Og3ewhrJDEgbpN15SmvsVFTh2T4NhMwwvWgHMgkciTDmjTT+7MMzzw6654/7WjTjdFic0dynsXnNtM8CdAylmJuP6a3HVtyA4QOoGzxW2IytUwgpgiZli/YNmq4U5ThSpz4XQ8AAbDYucfamna91+HqGYgIa5ReOczrUd8u

props

mnDQCIdgGIVN8ZJdABM7wqHpsHvhuhYrUo/Y2ldT4CDKwuR3JDq08/KePyT4UL9C4e9SfBpxlKa44v/86wDOTRigsizf7vp2pC9XOSzNWk8=

/*eslint no-param-reassign: ["error", { "props": false }]*/

function foo(bar) {
    bar.prop = "value";
}

function foo(bar) {
    delete bar.aaa;
}

function foo(bar) {
    bar.aaa++;
}

function foo(bar) {
    for (bar.aaa in baz) {}
}

function foo(bar) {
    for (bar.aaa of baz) {}
}

e1gy3cxJmCdWe+dm90ZS/dV/dL4EYEumJs/4xvQE083l4RZ/5tm3W91znd+gkQjtDyPI0a/sBR4iu42TdjY/zQ==

/*eslint no-param-reassign: ["error", { "props": true }]*/

function foo(bar) {
    bar.prop = "value";
}

function foo(bar) {
    delete bar.aaa;
}

function foo(bar) {
    bar.aaa++;
}

function foo(bar) {
    for (bar.aaa in baz) {}
}

function foo(bar) {
    for (bar.aaa of baz) {}
}

TZqQqyhkS62X5VCcymbe3qxyb5L0PlBy75xNHlP4EonitK8B3gEQf7ZeUd4QDHt80QLrHWRsEg/qiTkMChjQyQ67mWrV7i/O60/dDWezywW/NW0hCd3F2oIfi2UkejwvFveAfH67wZgvG9bKTXdwoFDbBnLtDbBVK2otYwCG3kY=

/*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["bar"] }]*/

function foo(bar) {
    bar.prop = "value";
}

function foo(bar) {
    delete bar.aaa;
}

function foo(bar) {
    bar.aaa++;
}

function foo(bar) {
    for (bar.aaa in baz) {}
}

function foo(bar) {
    for (bar.aaa of baz) {}
}

TZqQqyhkS62X5VCcymbe3qxyb5L0PlBy75xNHlP4EoleIlnbCj3uxwGzVqnopxQ5UDkYh6VMUOq8KzW4VkhWkjwFLnQAv138chTeIhJlpw+ntrJq2wISYq9wBpcqH+bKJRLvf5T0I0KIqsz2HS1OqLDMEAONWCpXY0tL6fOnsmg=

/*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsForRegex": ["^bar"] }]*/

function foo(barVar) {
    barVar.prop = "value";
}

function foo(barrito) {
    delete barrito.aaa;
}

function foo(bar_) {
    bar_.aaa++;
}

function foo(barBaz) {
    for (barBaz.aaa in baz) {}
}

function foo(barBaz) {
    for (barBaz.aaa of baz) {}
}

何时不使用

9OOdkW7zdOfS2Byvf6/srAkR/P1OWgkYyJ5iAc/SIg8OIY65OAgYW+3v2pPizdd8iL0bdxf/4Fkw0TrYGpPw0SM5Dw6rogaj4im070qTcPxBKOpPXrj5/5MDCQoe1CV4