You Could Lose Your Mind

What happens if you initialize an object with the same key (name) twice? I tried it in FireFox, Internet Explorer, Safari, and Opera.

In all cases, the last assignment (the rightmost one) is the winner.

var obj={a:1,b:2,a:3,b:4};

You end up with an object like this:

{a:3,b:4}

I’m not sure that’s mandated by the language spec. It might depend on the implementation. Anyone know?

1 Comment

  1. Ricardo said,

    October 26, 2008 at 10:10 pm

    It’s just logic/time. You can override variables or properties at any time, var assignments are executed in order, so you’re just overwriting it.


Leave a comment