Tân Nguyễn Tân Nguyễn - 1 month ago 12
Javascript Question

Do we need the rule "checking duplicate keys" in ESLint?

This example is coppied from disallow duplicate keys in object literals (no-dupe-keys):

var foo = {
bar: "baz",
bar: "qux"
};


When we call it:

alert(foo['bar']);


The string
qux
would be thrown. That also means,
foo['bar'] = "baz"
had been overriden, and it couldn't be assigned to. So, you should remove it.

Is it good?

I will take another example:

let element = $('<span/>').text('Hi!');
element.css({
display: '-webkit-box',
display: '-moz-box'
});


Well,
display: '-webkit-box'
would be overriden and couldn't be assigned, too?

No, it's useful on Chrome,
-moz-box
would be used on FF. So, they are different.

Then, the rule is breaking...

Should it be edited for future?

Answer

Yes, we do [need this rule].

Re-assigning a value in an object is at best useless and misleading, at worst it's an outright bug.

Your second example is still a plain old JS object and you're overwriting display.

Comments