如何在 JavaScript 中声明和使用动态变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5944749/
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
How do I declare and use dynamic variables in JavaScript?
提问by user735566
Suppose I need to declare a JavaScript variable based on a counter, how do I do so?
假设我需要基于计数器声明一个 JavaScript 变量,我该怎么做?
var pageNumber = 1;
var "text"+pageNumber;
The above code does not work.
上面的代码不起作用。
回答by bungdito
In JavaScript (as i know) there are 2 ways by which you can create dynamic variables:
在 JavaScript 中(据我所知)有两种方法可以创建动态变量:
- eval Function
- window object
- 评估函数
- 窗口对象
eval:
评估:
var pageNumber = 1;
eval("var text" + pageNumber + "=123;");
alert(text1);
window object:
窗口对象:
var pageNumber = 1;
window["text" + pageNumber] = 123;
alert(window["text" + pageNumber]);
回答by Mark Kahn
How would you then access said variable since you don't know its name? :) You're probably better off setting a parameter on an object, e.g.:
由于您不知道其名称,您将如何访问该变量?:) 您最好在对象上设置参数,例如:
var obj = {};
obj['text' + pageNumber] = 1;
if you -really- want to do this:
如果你 - 真的 - 想要这样做:
eval('var text' + pageNumber + '=1');
回答by Jayantha Lal Sirisena
I don't think you can do it sing JavaScript.I think you can use an array instead of this,
我不认为你可以用 JavaScript 来做到这一点。我认为你可以用一个数组代替这个,
var textArray=new Array();
textArray[pageNumber]="something";
回答by NT3RP
Assuming that the variable is in the global scope, you could do something like this:
假设变量在全局范围内,您可以执行以下操作:
var x = 1;
var x1 = "test"
console.log(window["x" + x]); //prints "test"
However, a better question might be why you want such behaviour.
但是,更好的问题可能是您为什么想要这种行为。
回答by KooiInc
You could also wrap your counter in an object:
您还可以将计数器包装在一个对象中:
var PageNumber = (function() {
var value = 0;
return {
getVal: function(){return value;},
incr: function(val){
value += val || 1;
this['text'+value]=true /*or some value*/;
return this;
}
};
})();
alert(PageNumber.incr().incr().text2); //=>true
alert(PageNumber['text'+PageNumber.getVal()]) /==> true
回答by Abhi
It can be done using this
keyword in JS:
可以this
在 JS 中使用关键字来完成:
Eg:
例如:
var a = [1,2,3];
for(var i = 0; i < a.length; i++) {
this["var" + i] = i + 1;
}
then when you print:
然后当你打印时:
var0 // 1
var1 // 2
var2 // 3
回答by George-Adrian Mocanu
I recently needed something like this.
我最近需要这样的东西。
I have a list of variables like this:
我有一个这样的变量列表:
var a = $('<div class="someHtml"></div>'),b = $('<div class="someHtml"></div>'),c = $('<div class="someHtml"></div>');
I needed to call them using another variable that held a string with the name of one of these variables like this:
我需要使用另一个变量来调用它们,该变量包含一个字符串,其中包含这些变量之一的名称,如下所示:
var c = 'a'; // holds the name of the wanted content, but can also be 'b' or 'c'
$('someSelector').html(eval(c)) // this will just use the content of var c defined above
Just use eval to get the variable data.
只需使用 eval 来获取变量数据。
I just did
我已经做了