❗deplicate parameters
🚧 施工中
JS ⟩ objects ⟩ built-in ⟩ Function ⟩ parameter ⟩ duplicate parameters
in non-strict mode, JavaScript allows a function to have several parameters with the same name, where later parameters shadow earlier parameters.
⭐️ duplicate parameters not allowed in strict mode.
function g(x, y, x, y) {
// ^
// ⛔ SyntaxError:
// Duplicate parameter name not allowed in this context
// (note: this is a compile-time error)
'use strict'; // ⭐️ strict mode
return [x, y];
}replit:duplicate parameters
// function with duplicate parameters
function f(x, y, x, y) {
return [x, y];
}
// x = 3
// ⭡ ↱ y = undefined
f(1, 2, 3 ) // [ 3, undefined ]duplicate parameters
mode FD FE AF
----------------------------------
strict ⛔️ ⛔️ ⛔️ // ⛔️ SyntaxError
non-strict ✅ ✅ ⛔️ // ✅ valid
----------------------------------
• FD: function declaration,
• FE: function expression,
• AF: arrow functionduplicate parameters Early Error rule (🚧) is always applied to arrow function definition even if 'strict mode' (🚧) is not defined explicitly.
duplicate parameters can cause ⛔️ early errors❗️
why does JavaScript even allow you to do such an obviously stupid thing in the first place?
Last updated
Was this helpful?