I just have a brief question about using spread operators on objects, when we are trying to return a copy of the object with a field changed. I've been working through a PluralSight tutorial having a lot to do with React and Redux, and I've run into this usage of the spread operator:
let original = {name: 'Joel', favoriteFood: 'oranges', age: 26};
let happyBirthday = {...original, age: 27};
Obviously this isn't the exact code from the example, but that is the usage I'm a little confused by. My initial understanding would be that this would result in an object with two age
keys, which obviously doesn't work. Does this work by just overwriting the value of the key that's duplicated? Could I say:
let obj = {a: 2, a: 'no actually 3', a: 'nevermind...', a: 2};
And then have obj.a
be 2
? This is a very small clarification that I'm looking for but I was thrown off when I saw this for some reason! Thanks!
var x = {a:1, b:2, c:3, c:4}; x;
and see what it outputs. Some times the easiest way to determine behaviour is just to try things. – Taplarlet obj = {a: 2, a: 'no actually 3', a: 'nevermind...', a: 3};
. obj.a will be 3 – tcf01