0
  RPGDATA = {
    turkey: 'Leg',
    chicken: 'Muffin'
}
var tempdata = RPGDATA;

    RPGDATA.turkey = 'NoLeg';

console.log(tempdata); // Why is this showing NoLeg? It should be 'Leg'?

console.log(RPGDATA);

On jsfiddle: http://jsfiddle.net/njxd7eLy/1/

When console logging tempdata, it is showing the properties of the new object with the changes applied?

Edit: Look here for an example: http://jsfiddle.net/zLeufxfm/ The old data is stored in that tempdata variable, but it is not with an object?

NiCk Newman
  • 1,716
  • 7
  • 23
  • 48
  • 1
    There is no new object. You're storing the *same* object in a second variable. If you want a new object, you need to be explicit about that, and copy all properties manually. – Bergi May 09 '15 at 16:40
  • Oh I know there is no new object, but why isn't the data that was unchanged being stored in tempdata? The data in tempdata hasn't been changed? – NiCk Newman May 09 '15 at 16:40
  • 2
    Your variable tempdata is just a reference to the object RPGDATA. – chRyNaN May 09 '15 at 16:42
  • Look here: http://jsfiddle.net/zLeufxfm/ It is not changed? – NiCk Newman May 09 '15 at 16:43
  • Strings are not objects. They are passed by value. – Siguza May 09 '15 at 16:44
  • See [this question](http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value). – Siguza May 09 '15 at 16:45
  • Aww i see now after reading that thread Siguaza, that helps me understand. I need to think of assigning variables as values only. – NiCk Newman May 09 '15 at 16:50
  • Also, this question has got to be a duplicate, I can't imagine this has never been asked on SO, but for the life of me I can't find anything. The only related post I found was [this](http://stackoverflow.com/questions/15214378/why-does-updating-properties-in-one-object-change-another-object), which has the same core problem, but a lot more stuff around it. – Siguza May 09 '15 at 16:53

2 Answers2

3

Objects in JavaScript are passed by reference. You need to clone the object if you want to change each object independently.

The JSON object can help streamline this.

http://jsbin.com/sonapebawo/1/edit?js,console

var myObj = {
  name: 'bob',
  age: '42'
};

var copy = JSON.parse(JSON.stringify(myObj));

myObj.newProp = 'Hello';

copy.otherProp = 'Yo';

console.log(copy, myObj);

Some documentation:

JSON Object

Working with objects

Oka
  • 23,367
  • 6
  • 42
  • 53
  • 'Objects in JavaScript are passed by reference.', that really make me sad lol. Anyways, I guess I need to re-parse, and do all that just for a clone... I'll mark u as best here soon. Thanks! – NiCk Newman May 09 '15 at 16:49
  • Objects in JavaScript are passed around a lot - and I mean **a lot** . Many complex systems depend on this for their functionality. Object-by-reference allows for a huge amount of flexibility in the language. I urge you to read up on the [Working with objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) article. – Oka May 09 '15 at 16:52
  • Oh I agree @Oka, I maybe just figured Javascript would of done this internally and JSON stringified it automatically to make it easier. The only way I can learn is if I try to find stuff that doesn't work or annoys me. And this is one case where it's annoying, but once I figured it out, I can utilize it. When I think of storing data in variables I need to think of storing VALUES only. If that makes sense lol. – NiCk Newman May 09 '15 at 16:54
1

The output should be both "NoLeg". As Oka's answer mentioned: Objects in JavaScript are passed by reference.

This diagram is straightforward, which will help understand how values are passed in JavaScript: http://huaban.com/pins/169758716/

coderz
  • 4,847
  • 11
  • 47
  • 70