2

Weird problem here, I'm trying to use a global function to update my settings object, example:

    var Settings = new Object;
    Settings.savepos = 'true';

    function UpdateSetting(obj,value){
        eval("Settings.obj = value");
        alert(Settings.savepos);
    }

The obj is the key of the object, meaning if I call the function with

UpdateSetting('savepos','false')

the alert will always just give me true, how do I convert that eval or any alternative so it will update settings object's key with the value?

4 Answers 4

1

You are setting Settings.obj, not setting.savepos.

Try this instead:

function UpdateSetting(obj,value){
    Settings[obj] = value;
    alert(Settings.savepos);
};
Sign up to request clarification or add additional context in comments.

Comments

1

You are always changing the "obj" key of the object to equal value, which is likely to be undefined (or, at least, not defined to what you want) in the context eval() executes it in. So, you have two options. First, you can keep using eval() (although i don't recommend it because it's more pain than necessary):

var Settings = new Object;
Settings.savepos = 'true';

function UpdateSetting(obj,value){
    eval("Settings."+obj+" = '"+value+"'");
    alert(Settings.savepos);
}

Or, as numerous other have suggested, you can use the array operator[] to access the property by key:

var Settings = new Object;
Settings.savepos = 'true';

function UpdateSetting(obj,value){
    Settings[obj] = value;
    alert(Settings.savepos);
}

Comments

1
  1. you dont need an eval
  2. you're setting .obj, not .savepos (there is no interpolation for the string)
  3. you may be calling it wrong.

I'm not exactly sure why you don't just set the value directly (eg. Settings.savepos=false;).
You can attach the function to that object to do something similar:

var Settings            =  new Object;
Settings.savepos        =  true;
Settings.UpdateSetting  =  function (prop,value){this[prop] = value;}

Settings.UpdateSetting('savepos',false);

2 Comments

If you were to have multiple Settings objects, then you should consider using the prototype method (google: 'javascript prototype method'). It's a little bit harder to search for it because there's also a 'prototype' JavaScript framework. Glad this helped :)
I wanted to note that I still encourage setting the value directly eg var prop = 'savepos'; Settings[prop] = true;, but I also know that people have their reasons for asking the question and wanting it to be done a certain way.
0

You should be able to use array notation on the object. Underneath it's just a keyed hash.

Try:

Settings[obj] = value;

I'd also suggest passing values as they are, i.e. string, int, etc:

UpdateSetting('key_name', false);

1 Comment

I believe the technical definition is subscript notation

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.