Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is object with duplicated property accepted in JavaScript?

Tags:

javascript

I was expecting JavaScript to reject objects with duplicated properties as invalid but it accepts them in some cases.

{"a":4,"a":5} results in an SyntaxError at least in Firefox and Chrome which seems obvious due to the property a being defined twice.

However ({"a":4,"a":5}) evaluates just fine and results in an object {"a":5} in both Firefox and Chrome.

Why is the expression with the parenthesis accepted?

Summing up the responses: The first example is simply not the construction of an object but a block of labeled statements. Duplicated properities in objects are perfectly valid in which case the last definition wins.

Thanks a lot for your answers!

like image 626
Augustus Kling Avatar asked Nov 08 '12 09:11

Augustus Kling


People also ask

Can JavaScript object have duplicate keys?

No, JavaScript objects cannot have duplicate keys. The keys must all be unique.

Can we have two properties with the same name inside an object?

You cannot. Property keys are unique. Follow TravisJ 's advice. You might want to look up the term 'multimap', too.

What are the two kinds of objects properties we can have in JavaScript?

JavaScript objects have two types of properties: data properties and accessor properties.

How do you copy properties from one object to another JS?

Assigning. The Object. assign() function can be used to copy all enumerable own properties from one or more source objects to a target object. This function returns the target object to the newObject variable.


2 Answers

It is perfectly legal in ECMAScript 3 to declare duplicate properties in an object literal; the SyntaxError you get probably comes from the fact that you used an object literal as a statement, which is not possible due to the confusion with block statements ({ doSomething(); }).

If you want this to be reported as an error, you may want to switch to ECMAScript 5's strict mode: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode.

like image 162
Julien Royer Avatar answered Oct 06 '22 19:10

Julien Royer


What you state has no problem if you assign it to a variable, if you don't however, you get the error you mention. Which makes all the difference from a syntax point of view.

When you wrap any structure in parens you are causing that syntax to be evaluated as an expression, the result of which is stored as a temporary variable. The error I get when not doing so in Firefox is unexpected label or invalid label, so it seems without assignment, or parens, this object construction is not treated as an object construction - instead it is treated as a block with multiple label statements that are defined illegally:

{
  a: function(){
    alert('a');
  },
  b: function(){
    alert('b');
  }
}

The above should be totally acceptable as an object, however you get a similar error if you evaluate it without assinging it to some form of variable, or evaluating it with parens. Put simply the duplication of the attribute name is not causing the error :)

Basically imagine your first example, but like this:

function (){
  "a": 4,
  "b": 5
}

That is roughly how these browsers are treating it, which is now obviously illegal javascript syntax... whereas it wasn't so obvious before.

like image 36
Pebbl Avatar answered Oct 06 '22 17:10

Pebbl