I'm trying to figure this javascript variable referencing issue out. Here is a fiddle to demonstrate what I'm about to explain: http://jsfiddle.net/XpVb5/1/
I have an object that I want to define and call within a separate object's properties.
var vals = {'something':1}; //original object
var buf = {'field1': vals, 'field2': vals}; //second object with original object used as properties
Now I want to change the something
property for only field1
, so naturally, I would do this:
buf.field1.something = 0;
However, this will also change field2
's something
property. I'm assuming that this is because of how Javascript references variables in the variable definition process. But, in either case, how can I get around this without explicitly calling {'something':0}
each time I need to use it in a property definition; like so:
var buf = {'field1': {'something':0}, 'field2': {'something':1}};