Is there a way to find JavaScript variable on the page (get it as an object) by its name? Variable name is available as a string constant.
7 Answers
<script>
var a ="test";
alert(a);
alert(window["a"]);
alert(eval("a"));
</script>
3 Comments
eval for this.eval?All JS objects (which variables are) are available within their scope as named properties of their parent object. Where no explicit parent exists, it is implicitly the window object.
i.e.:
var x = 'abc';
alert(window['x']); //displays 'abc'
and for a complex object:
var x = {y:'abc'};
alert(x['y']); //displays 'abc'
and this can be chained:
var x = {y:'abc'};
alert(window['x']['y']); //displays 'abc'
5 Comments
If you are wanting a variable that is declared in the global context, it is attached to the window object. ex: window["variableName"]. All variables are a hash table value within their scope.
If you have to use dotted notation, then you will want to follow kennebec's suggestion, to navigate through the object hierarchy. eval() can work as well, but is a more expensive operation than is probably needed.
Comments
If it's a global variable, you can look it up by name on the global object, since global variables are properties of the global object. On browsers, there's a global variable that refers to the global object called window, so:
var name = "foo";
window.foo = 42;
alert(Number(window[name])); // 42
But global variables are a Bad Thing(tm).
To do this without globals, use your own object:
var name = "foo";
var obj = {};
obj.foo = 42;
alert(Number(obj[name])); // 42
Both of the above work because in JavaScript, you can refer to an object property either with dot notation and a literal (obj.foo), or with bracketed notation and a string (obj["foo"]), and in the latter case, the string can be the result of any expression, including a variable lookup.
5 Comments
obj would be the variable to be accessed (and not obj.foo) - would eval be the only option then?obj were a global, you could use window["obj"]. If it weren't, then you'd have to use eval. But I wouldn't, I'd change my code to put obj in a container so I could just look it up in the normal way.eval statement, which then I could retrieve only through a second eval - but then changed the code that a container variable is created before the first eval; eliminating the need for the second eval.eval, you should be able to access it normally: eval("var x = 42;"); console.log(x); shows 42 in the console. (But there's almost never any reason to use eval.)If your string references a 'deep' property of a global, like 'Yankee.console.format' you can step through the references:
String.prototype.deref= function(){
// remove leading and trailing quotes and spaces
var obj= this.replace(/(^[' "]+|[" ']+$)/g,'');
var M= obj.match(/(^[\w\$]+(\.[\w\$]+)*)/);
if(M){
M= M[1].split('.');
obj= window[M.shift()];
while(obj && M.length) obj= obj[M.shift()];
}
return obj || this;
}
Comments
var getVar = function (obj) {
for(var key in this) {
if(obj === this[key]) return key;
}
};
foo = 'foo';
console.log( getVar(foo) ); // => 'foo'
2 Comments
foo do helps to followYou could use eval()
8 Comments
eval and with like the plague. It should be OK to express dislike towards your solution. I would never post eval as a solution, unless there was no other, or it was just too cumbersome