简体   繁体   中英

Getting value of var by its name

I'm trying get value of var by its name:

var1='Hello'

var2 = getTextFromFirstTextBox() //text in label is "var1"

var3 = ${var2}

Everything works, but here I should get 'hello' but it doesn't work

var4 = ${var3} 

What I must to do to set value of value1 to value4?

var1 = 'Hello'
var2 = getTextFromFirstTextBox() //text in label is "var1"

Now, var2 = "var1" . If you want the value of var1 assigned to var3 you can:

var3 = window[var2];

EDIT
See @jleedev's answer for the scope issue raised in the comments.

You can access global variables as properties of window :

> var1='Hello'
"Hello"
> var2='var1'
"var1"
> window[var2]
"Hello"

This is similar to Python, by the way — based on your question it looks like you're expecting something like PHP's variable variables.

It's much better to explicitly create an object for storing what you need:

> dict = {} // Create a new Object
Object
> dict['var1'] = 'Hello' // Index it with a string
"Hello"
> dict.var1 // Or directly if you know the name you want
"Hello"

Let's take this one line from your question:

var1='Hello'

As it stands, that will create a global variable (regardless of where the code is). Global variables are properties of the global object, which is window on browsers, so you can access that via bracketed notation with a string (eg, window["var1"] ) as a couple of the answers here mention. But that's only part of the story.

If the code is at global scope, it's fairly obvious you're creating a global variable. If that line appears as-is in a function , it still creates a global variable — you're falling prey to the Horror of Implicit Globals .

The moral of the story is: Use var when creating variables:

var var1 = 'Hello';

Now you're explicitly creating a variable in the current scope — eg, at global scope if that's where the code is, or at function scope if that's where the code is.

So global variables are properties of window ; how 'bout function variables, are they properties of an object? Yes, they are (it's called the "variable object" in the spec, and it's a very real thing, not a spec abstraction), but you have no means of accessing that object directly, there's no symbol or name you can use to get at it. If you want to get a function's variable using a string version of its name, your only option is eval , and don't do that. Instead, refactor the code and make the thing you need to access the property of an object:

var thingsToLookUp = {
    var1: "Hello",
    var2: "There"
};
alert(thingsToLookUp["var1"]); // alerts "Hello"

Variables are properties of their parent object, Global variables can be handled as properties of the window object so you can do something like this:

var var1, var2, var3;
function getTextFromFirstTextBox(){
    return "var1";
}
var1 = 'Hello';
var2 = getTextFromFirstTextBox();
var3 = window[var2];
alert(var3); // Shows: Hello

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM