Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!


Duplicate member '{a}'

When do I get this error?

JSHint (below version 1.0.0) will throw the "Duplicate member '{a}'" error when it encounters an object literal that contains more than one property with the same identifier. In the following example we attempt to assign an object containing two properties with the identifier y to a variable x:

Why do I get this error?

This error is raised to highlight code that may not work as you expect and a possible syntax error. In strict mode, your code will raise a syntax error. Otherwise, it will run without error but you will most likely get unexpected results.

The specification states the following, where previous is the result of calling [[GetOwnProperty]] on the object in question with the identifier we are trying to add to it (ES5 §11.1.5):

If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true
    a. This production is contained in strict code...

So, when your code is running in strict mode, a syntax error will be thrown if you attempt to define multiple properties with the same identifier. When not in strict mode, no error is thrown but usually the latest definition will override any earlier definitions and you may experience some strange bugs if the naming was unintentional. Another good reason to always run your code in strict mode.

Note: if you are using JSLint instead of JSHint, this error will present itself as "Duplicate '{a}'", which is more generic and used to cover various "duplicate" situations.

In JSHint 1.0.0 and above this warning has changed to "Duplicate key '{a}'". More detail can be found the page dedicated to that message.


comments powered by Disqus