Pattern: Duplicate key in object literal
Issue: -
Multiple properties with the same key in object literals cause the last instance to silently overwrite previous ones. This often indicates a copy-paste error or typo and can lead to unexpected behavior.
Example of incorrect code:
var foo = {
bar: "baz",
bar: "qux" // Overwrites previous bar
};
const config = {
name: "project",
version: "1.0.0",
name: "test" // Overwrites previous name
};
var obj = {
0x1: "baz",
1: "qux" // Same as 0x1 in decimal
};
Example of correct code:
var foo = {
bar: "baz",
qux: "qux"
};
const config = {
name: "project",
version: "1.0.0",
code: "test"
};