2

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.

http://jsfiddle.net/Pwqeu/

2 Answers 2

2

The line

reference.colors = colors2;

means you have access to reference.colors, even though you don't have access to colors, right? So rather than

var colors2 = [];
// etc

do

var colors2 = reference.colors;
// modify to your desired array
colors2.length = 0; // "reset" it
colors2.push('white');

Now back in scope with colors,

console.log(colors); // ['white']
Sign up to request clarification or add additional context in comments.

3 Comments

Great thought. I think this will work. So basically modify the object directly instead of trying to point to a new object. One last bit, using this technique do you think it would be good to itterate over colors and delete all properties, and then add new properties?
If it's just items in the Array, simply set length 0 to remove them all in one go.
Right, using a obj with properties. So right now I am using a for(in) delete and then another for(in) to link each property. Its working great! Thanks, this answer was amazing!
0

I believe Paul answered your question, so I'm just going to attempt to break down what you originally had to further help you. Variables don't reference other variables they reference the objects the variables point to.

var colors = [];  // colors points to an Array Object
colors.push('red'); 
colors.push('yellow');

//Create a reference to colors
var reference = {};         // reference points to a new Object
reference.colors = colors;  // reference.colors now points to the array Object colors points to.

//Add another array of colors
var colors2 = [];          // colors2 points to new Array Object
colors2.push('white');

//Change reference to point to colors2
reference.colors = colors2;   // this statement points reference.colors to the Array Object colors2 points to. So now you will not have access to the Array object colors pointed to.

console.log(reference.colors);
console.log(colors); //Would like it to log 'white'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.