按名称访问 JavaScript 变量的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4399794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access value of JavaScript variable by name?
提问by Torben
Hello it is possible to access the value of a JavaScript variable by name? Example:
您好,可以通过名称访问 JavaScript 变量的值吗?例子:
var MyVariable = "Value of variable";
function readValue(name) {
....
}
alert(readValue("MyVariable"));
Is this possible, so that the output is "Value of variable"? If yes, how do I write this function?
这可能吗,以便输出是“变量值”?如果是,我该如何编写这个函数?
Thanks
谢谢
回答by Spiny Norman
Global variables are defined on the window
object, so you can use:
window
对象上定义了全局变量,因此您可以使用:
var MyVariable = "Value of variable";
alert(window["MyVariable"]);
回答by wsanville
Yes, you can do it like this:
是的,你可以这样做:
var MyVariable = "Value of variable";
alert(window["MyVariable"]);
回答by Dr DIASOLUKA
var MyVariable = "Value of variable";
alert(readValue("MyVariable"));
// function readEValue(name) { readevalue -> readvalue // always do copy-paste to avoid errors
function readValue(name) {
return window[name]
}
That's all about ;o)
这就是全部;o)
回答by lyon819
var sen0=1;
if(window["sen"+n] > 0){
}
回答by shunz19
Try this ^_^
试试这个^_^
var MyVariable = "Value of variable";
alert(readValue("MyVariable"));
function readValue(name) {
return eval(name)
}
回答by Chris Jeffries
I tried the function below which was posted by Nicolas Gauthier via Stack Overflow to get a function from a string by naming it, and when used with the name of a variable, it returns the variable's value.
我尝试了下面由 Nicolas Gauthier 通过 Stack Overflow 发布的函数,通过命名从字符串中获取函数,当与变量名一起使用时,它返回变量的值。
It copes with dotted variable names (values of an object). It works with global variables and variables declared with var, but NOT with variables defined with 'let' which are not visible in called functions.
它处理带点的变量名(对象的值)。它适用于全局变量和用 var 声明的变量,但不适用于用 'let' 定义的变量,这些变量在被调用的函数中不可见。
/***
* getFunctionFromString - Get function from string
*
* works with or without scopes
*
* @param string string name of function
* @return function by that name if it exists
* @author by Nicolas Gauthier via Stack Overflow
***/
window.getFunctionFromString = function(string)
{
let scope = window; let x=parent;
let scopeSplit = string.split('.');
let i;
for (i = 0; i < scopeSplit.length - 1; i++)
{
scope = scope[scopeSplit[i]];
if (scope == undefined) return;
}
return scope[scopeSplit[scopeSplit.length - 1]];
}
回答by ssoward
//Ran into this today
//Not what I want
var test = 'someString';
var data = [];
data.push({test:'001'});
console.log(test); --> 'someString';
console.log(data); -->
(1) [{…}]
0: {test: "001"} //wrong
//Solution
var obj = {};
obj[test] = '001';
data.push(obj);
console.log(data); -->
(2)?[{…}, {…}]
0: {test: "001"} //wrong
1: {someString: "001"} //correct