JavaScript allows duplicate property names in classes and object literals. The last occurrence will overwrite previous definitions. Therefore,
having more than one occurrence will have no effect and may cause misunderstandings and bugs.
let data = {
"key": "value",
"1": "value",
"key": "value", // Noncompliant - duplicate of "key"
'key': "value", // Noncompliant - duplicate of "key"
key: "value", // Noncompliant - duplicate of "key"
\u006bey: "value", // Noncompliant - duplicate of "key"
"\u006bey": "value", // Noncompliant - duplicate of "key"
"\x6bey": "value", // Noncompliant - duplicate of "key"
1: "value" // Noncompliant - duplicate of "1"
}
class Foo {
bar() { }
bar() { } // Noncompliant - duplicate of "bar"
}
Defining a class
with a duplicated constructor
will generate an error.
class Class {
constructor() {
}
constructor(value) { // Noncompliant: A class may only have one constructor
}
}
Before ECMAScript 2015, using duplicate names generated an error in JavaScript strict mode code.
This rule will also report on duplicate properties in JSX.
function MyComponent(props) {
return <div prop={props.prop1} prop={props.prop2}> { /* Noncompliant, 'prop' is defined twice */ }
This is my component
</div>;
}