Skip to content

SyntaxError: Duplicate 参数 name 不允许 in 这个上下文

错误信息

                Duplicate 参数 name 不允许 in 这个上下文
              

快速摘要

你 declared 函数使用 duplicate 参数 names 虽然 in strict 模式 — 这个是 不允许.

为什么发生

  • 函数有 two 参数使用相同 name in strict 模式

  • arrow 函数 or 类方法有 duplicate 参数 names

  • Code 是 converted 到 strict 模式 (e.g., 通过添加 'use strict' or using ES 模块)

最小示例

✗ 错误代码
"use strict";
function add(a, a) {
  return a + a;
}
✓ 修复代码
"use strict";
function add(a, b) {
  return a + b;
}

In strict 模式, duplicate 参数 names 是不 允许. Rename 其次参数.

如何诊断

  • 检查函数's 参数列表 for duplicate names

  • 验证如果 code 是运行 in strict 模式 (模块 and 类总是是)

  • Look for 参数那个是 accidentally given 相同 name

如何修复

  • Rename one 的 duplicate 参数

  • Use REST 参数 or options 对象相反的 许多参数

如何预防

  • Use descriptive, 唯一参数 names

  • Consider using options 对象 for 函数使用许多参数

相关资源

相关术语

相关课程

  • 函数

    学习关于函数参数.

← 返回语言