I am having some difficulties wrapping my head around how to properly change the reference of an object without immediate access to that object. Consider the following code. Is it possible to change the value of the colors array without setting it directly?
//Add some colors
var colors = [];
colors.push('red');
colors.push('yellow');
//Create a reference to colors
var reference = {};
reference.colors = colors;
//Add another array of colors
var colors2 = [];
colors2.push('white');
//Change reference to point to colors2
reference.colors = colors2;
console.log(reference.colors);
console.log(colors); //Would like it to log 'white'
Trying to avoid writing the following code.
colors = colors2;
I understand that reference is just pointing from one array to another. But I can't figure out a way to do it other than what I've shown above.
Any ideas or suggestions are welcomed.