2

To remove tedious code i would like to create a function to simplify it.
So to change the settings that i stored in an object, I created this.

var settings = {
 color : 'red'
 //one of many settings
};
alert(settings.color); // returns red
function changeValue(property, value){
 settings.property = value;
}
changeValue('color', 'blue');
alert(settings.color); // still return red

it seems like its treated as a string instead.
And if i try this:

function changeValue(object, value){
 object = value;
}
changeValue(settings.color, 'blue');

it would only give the function the value which is 'red'.
and this:

changeValue('settings.color', 'blue');

would not work obviously.

So how do i solve this ? How do i pass down a variable to a function but not its value so it can be altered ?

Note: the function is simplified for now, but would contain a lot more magic when it works.

asked Dec 26, 2011 at 21:39

2 Answers 2

2

Pass the object and the name of the property that you want to change as separate arguments:

function changeValue(obj, prop, value) {
 obj[prop] = value;
}
changeValue(settings, "color", "blue");

Assuming var prop = "color" then obj[prop] is equivalent to obj.color. The first notation allows you to dynamically specify the property that should be changed.

answered Dec 26, 2011 at 21:43
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers. New to javascript so i had no idea of this method.
1

when you do settings.color you get a value, rather than object. You'd have to do something like

function changeValue(object, field, value){
 object[field] = value;
}

used like

changeValue(settings, 'color', 'blue')

which may not actually reduce time for you at all..


EDIT:: I have made this function for you. But please.. Don't ever use it.

var settings = {
 color : "blue"
}
function setField(str, value){
 var args = str.split(".").reverse();
 var obj = window[args.pop()];
 while(args.length - 1 > 0) 
 obj = obj[args.pop()]
 obj[args.pop()] = value;
}
alert(settings.color);
setField("settings.color", "red");
alert(settings.color);
answered Dec 26, 2011 at 21:43

3 Comments

since i wanted to "remove tedious code", this would definitely go against it.
@EinarLöve 'tedious code'? My 'solution' does exactly what you want. setField("settings.color", "red") works as you wanted. I also gave the 'correct' way of doing it. Although your original requirement is wrong, and both solutions should not be used (including lwburk's code (which is identical to mine))
You first function is definitely just what im looking for. my previous comment was a respond to your edit.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.