no-param-reassign

禁止重新分配函数参数

分配给声明为函数参数的变量可能会产生误导并导致令人困惑的行为,因为修改函数参数也会在不处于 strict 模式时改变 arguments 对象(请参阅下面的 何时不使用)。通常,分配给函数参数是无意的,并且表示错误或程序员错误。

¥Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object when not in strict mode (see When Not To Use It below). Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

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

¥This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

规则详情

¥Rule Details

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

¥This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-param-reassign: "error"*/

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

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

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

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

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-param-reassign: "error"*/

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

选项

¥Options

该规则采用一个选项,一个带有布尔属性 "props" 的对象以及数组 "ignorePropertyModificationsFor""ignorePropertyModificationsForRegex""props" 默认为 false。如果 "props" 设置为 true,则此规则会警告不要修改参数属性,除非它们包含在 "ignorePropertyModificationsFor""ignorePropertyModificationsForRegex" 中,默认情况下为空数组。

¥This rule takes one option, an object, with a boolean property "props", and arrays "ignorePropertyModificationsFor" and "ignorePropertyModificationsForRegex". "props" is false by default. If "props" is set to true, this rule warns against the modification of parameter properties unless they’re included in "ignorePropertyModificationsFor" or "ignorePropertyModificationsForRegex", which is an empty array by default.

props

默认 { "props": false } 选项的正确代码示例:

¥Examples of correct code for the default { "props": false } option:

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

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

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

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

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

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

{ "props": true } 选项的错误代码示例:

¥Examples of incorrect code for the { "props": true } option:

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

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

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

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

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

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

设置 "ignorePropertyModificationsFor"{ "props": true } 选项的正确代码示例:

¥Examples of correct code for the { "props": true } option with "ignorePropertyModificationsFor" set:

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

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

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

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

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

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

设置 "ignorePropertyModificationsForRegex"{ "props": true } 选项的正确代码示例:

¥Examples of correct code for the { "props": true } option with "ignorePropertyModificationsForRegex" set:

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

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

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

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

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

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

何时不使用

¥When Not To Use It

如果你想允许分配给函数参数,那么你可以安全地禁用此规则。

¥If you want to allow assignment to function parameters, then you can safely disable this rule.

strict 模式代码不会将参数对象的索引与每个参数绑定同步。因此,此规则对于防止 ESM 模块或其他 strict 模式函数中的参数对象突变不是必需的。

¥strict mode code doesn’t sync indices of the arguments object with each parameter binding. Therefore, this rule is not necessary to protect against arguments object mutation in ESM modules or other strict mode functions.

版本

此规则是在 ESLint v0.18.0 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号