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"*/
const foo = function(bar) {
bar = 13;
}
const foo1 = function(bar) {
bar++;
}
const foo2 = function(bar) {
for (bar in baz) {}
}
const foo3 = function(bar) {
for (bar of baz) {}
}
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint no-param-reassign: "error"*/
const foo = function(bar) {
const baz = bar;
}
选项
🌐 Options
此规则接受一个选项对象,该对象具有布尔属性 "props",以及数组 "ignorePropertyModificationsFor" 和 "ignorePropertyModificationsForRegex"。"props" 默认为 false。如果 "props" 设置为 true,该规则会警告除非参数属性包含在 "ignorePropertyModificationsFor" 或 "ignorePropertyModificationsForRegex" 中,否则不应修改它们,"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 }]*/
const foo = function(bar) {
bar.prop = "value";
}
const foo1 = function(bar) {
delete bar.aaa;
}
const foo2 = function(bar) {
bar.aaa++;
}
const foo3 = function(bar) {
for (bar.aaa in baz) {}
}
const foo4 = 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 }]*/
const foo = function(bar) {
bar.prop = "value";
}
const foo1 = function(bar) {
delete bar.aaa;
}
const foo2 = function(bar) {
bar.aaa++;
}
const foo3 = function(bar) {
for (bar.aaa in baz) {}
}
const foo4 = 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"] }]*/
const foo = function(bar) {
bar.prop = "value";
}
const foo1 = function(bar) {
delete bar.aaa;
}
const foo2 = function(bar) {
bar.aaa++;
}
const foo3 = function(bar) {
for (bar.aaa in baz) {}
}
const foo4 = 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"] }]*/
const foo = function(barVar) {
barVar.prop = "value";
}
const foo1 = function(barrito) {
delete barrito.aaa;
}
const foo2 = function(bar_) {
bar_.aaa++;
}
const foo3 = function(barBaz) {
for (barBaz.aaa in baz) {}
}
const foo4 = 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 模式代码不会将 arguments 对象的索引与每个参数绑定同步。因此,这条规则对于在 ESM 模块或其他 strict 模式函数中防止 arguments 对象的修改并不是必要的。
版本
此规则是在 ESLint v0.18.0 中引入。