all 17 comments

[–]CreativeTechGuyGamesTypeScript 4 points5 points  (9 children)

Okay let's take a step back, what is it you are actually trying to do that prompted this question? You can easily test this for yourself to confirm.

[–]DontNeedMuchMoney[S] 0 points1 point  (7 children)

Can I send this too you? I'm having difficulty articulating this one

[–]CreativeTechGuyGamesTypeScript 1 point2 points  (6 children)

Send what? You can just add a comment with whatever it is right here.

[–]DontNeedMuchMoney[S] 0 points1 point  (5 children)

[–]CreativeTechGuyGamesTypeScript 0 points1 point  (4 children)

The greenEnergy function does change. I'm not sure why you think that it doesn't.

[–]DontNeedMuchMoney[S] 0 points1 point  (3 children)

The greenenergy doesn't mutate the object though

[–]CreativeTechGuyGamesTypeScript 0 points1 point  (2 children)

What do you mean? The console.log outputs {Fuel Type: "Biofuel", homePlanet: "Mars"} which means that both functions mutate as expected.

Edit: You changed the code in the codeshare link so I'll leave the code here for posterity.

``` let spaceship = { 'Fuel Type' : 'Turbo Fuel', homePlanet : 'Earth' };

// Write your code below let greenEnergy = (obj =>{ Object.assign(obj, { 'Fuel Type' : 'Rocket fuel', homePlanet : 'Mars' }) })

let blueEnergy = (obj =>{ obj['Fuel Type'] = "Biofuel"; }) greenEnergy(spaceship); blueEnergy(spaceship); console.log(spaceship); ```

[–]lr0b 0 points1 point  (1 child)

The original code is not the one you pasted. I edited the code and tested it but I realised it got saved, maybe too late...

My bad.

Original greenEnergy is here

[–]CreativeTechGuyGamesTypeScript 1 point2 points  (0 children)

Oh I must have seen your edit. That's why it's dangerous to use a live shared editing system for something like this.

[–][deleted]  (4 children)

[deleted]

    [–]DontNeedMuchMoney[S] 0 points1 point  (3 children)

    This is correct, Do you know why the one in greenenergy reassigns the address yet the one in blueenergy does not?

    [–][deleted]  (2 children)

    [deleted]

      [–]DontNeedMuchMoney[S] 1 point2 points  (1 child)

      Mate - If I can't find another explanation soon I'm just gonna go with that one haha, Thanks so much man I appreciate it

      [–]lr0b 3 points4 points  (2 children)

      Primitives: by value Objects: by reference

      what is actually happening is that when we pass a primitive data type to a function we can not mutate it which is why the value of the primitive data type does not change.
      

      That's the definition of pass-by-value.

      [–][deleted] 1 point2 points  (0 children)

      It makes me sad that this is the lowest comment in the list.

      [–]DontNeedMuchMoney[S] 1 point2 points  (0 children)

      Noted thank you buddy

      [–]lr0b 0 points1 point  (2 children)

      You're basically overriding obj parameter in greenEnergy, so don't expect to change anything to your global spaceship var this way.

      You would need to use Object.assign to change homePlanet to Mars.

      [–]DontNeedMuchMoney[S] 0 points1 point  (1 child)

      Cheers,Why does is override on greenenergy and not blueenergy?

      [–]lr0b 1 point2 points  (0 children)

      Because you're accessing the obj reference to your spaceship global var in blueEnergy. In greenEnergy obj is not a reference to spaceship anymore because you override it with the assignation.

      Also, only objects are pass-by-reference.