To keep the global namespace clean, my JavaScript code is wrapped like this:
(function() {
/* my code */
})();
Now I have some variables declared in this scope which I want to access using a variable variable name (e.g. the name is 'something' + someVar
).
In the global scope I'd simply use window['varname']
, but obviously that doesn't work.
Is there a good way to do what I want? If not I could simply put those variables inside an object to use the array notation...
Note: eval('varname')
is not an acceptable solution. So please don't suggest that.
This is a good question because
this
doesn't point to the anonymous function, otherwise you would obviously just usethis['something'+someVar]
. Even using the deprecatedarguments.callee
doesn't work here because the internal variables aren't properties of the function. I think you will have to do exactly what you described by creating either a holder object...