[英]How do I declare and use dynamic variables in JavaScript?
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.上面的代码不起作用。
In JavaScript (as i know) there are 2 ways by which you can create dynamic variables:在 JavaScript 中(据我所知)有两种方法可以创建动态变量:
eval:评估:
var pageNumber = 1;
eval("var text" + pageNumber + "=123;");
alert(text1);
window object:窗口对象:
var pageNumber = 1;
window["text" + pageNumber] = 123;
alert(window["text" + pageNumber]);
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, eg:
:) 您最好在对象上设置参数,例如:
var obj = {};
obj['text' + pageNumber] = 1;
if you -really- want to do this:如果你 - 真的 - 想要这样做:
eval('var text' + pageNumber + '=1');
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";
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.但是,更好的问题可能是您为什么想要这种行为。
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
It can be done using this
keyword in JS:可以在 JS 中使用
this
关键字来完成:
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
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我已经做了
I know a lot of the other answers work great, such as window["whatever"] = "x";我知道很多其他答案都很好用,例如 window["whatever"] = "x"; but I will still put my own answer here, just in case it helps.
但我仍然会把我自己的答案放在这里,以防万一。
My method is to use Object.assign:我的方法是使用 Object.assign:
let dict = {};
dict["test" + "x"] = "hello";
Object.assign(window, dict)
a little improvement over bungdito's answer, use the dynamic variable dynamically对 bungdito 的答案稍作改进,动态使用动态变量
var pageNumber = 1;
eval("var text" + pageNumber + "=123456;");
eval(`alert(text${pageNumber})`);
note: usage of eval
is strongly discourgae注意:强烈反对使用
eval
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.