The object exists in some scope, so you can almost always access the variable via this syntax:
var objname = "myobject";
containing_scope_reference[objname].some_property = 'some value';
The only place where this gets tricky is when you are in a closed scope and you want access to a top-level local variable. When you have something like this:
(function(){
var some_variable = {value: 25};
var x = "some_variable";
console.log(this[x], window[x]); // Doesn't work
})();
You can get around that by using eval
instead to access the current scope chain ... but I don't recommend it unless you've done a lot of testing and you know that that's the best way to go about things.
(function(){
var some_variable = {value: 25};
var x = "some_variable";
eval(x).value = 42;
console.log(some_variable); // Works
})();
Your best bet is to have a reference to a name in an always-going-to-be-there object (like this
in the global scope or a private top-level variable in a local scope) and put everything else in there.
Thus:
var my_outer_variable = {};
var outer_pointer = 'my_outer_variable';
// Reach my_outer_variable with this[outer_pointer]
// or window[outer_pointer]
(function(){
var my_inner_scope = {'my_inner_variable': {} };
var inner_pointer = 'my_inner_variable';
// Reach my_inner_variable by using
// my_inner_scope[inner_pointer]
})();