9

I just found that in the last Chrome 42 and FF 37.0.2 this lines of code are perfectly legal

"use strict";
var o = { p: 1, p: 2 };

(copy-pasted from MDN )

In IE 10-11 and Opera 28.0.1750 it throws error as expected.

In the same time,

abc=0;

causes error (undeclared variable) as expected.

Does anybody know what caused such change?

Andrey Kuleshov
  • 645
  • 6
  • 19
  • 1
    See [this discussion on why the behavior changed](https://esdiscuss.org/notes/2014-06-06#rest-properties-and-spread-properties-sebastian-markb-ge-). – Benjamin Gruenbaum Apr 29 '15 at 12:00
  • 1
    closely related: [What's the purpose of allowing duplicate property names?](https://stackoverflow.com/q/30617139/1048572) – Bergi Jun 18 '19 at 21:58

1 Answers1

14

There is a Bugzilla ticket here. From what I gather (here and other pages I have looked up), duplicate properties are legal in ECMAScript version 6, opposed to ES5, where it is forbidden in strict mode.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • Absolutely https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Duplicate_property_names – Kaiido Apr 29 '15 at 07:33
  • 6
    That really looks like a bad bad bad design decision. – Andrey Kuleshov Apr 29 '15 at 07:35
  • @Kaiido yes, I got it. The point is - that does not work yet :) Moreover And when it will work... I do not really believe that feature is really required and can not be workarounded. On the other hand, I - personally - met problems with properties duplication... – Andrey Kuleshov Apr 29 '15 at 07:43
  • @Kaiido No, beacuse of `{...obj, ...objPatch}` – Zydnar May 27 '18 at 18:41
  • Yes, in some situations it would be nice to put a duplicate "patch" value at the end of a literal object property list string. But that rare advantage is not worth losing the wonderful feature of objects that they guarantee no duplicate keys. Programming requires unique keys far more often than non-unique keys. Unique keys have been a major aspect of semantic association arrays since the beginnings of computer science. – David Spector Jun 22 '20 at 16:48