[英]How do I create a variable in Javascript, and add a changing number to the variable's name?
This code is completely wrong, but I hope it gives you the idea of what I'm going for: 这段代码是完全错误的,但是我希望它能使您了解我要做什么:
var qarowquestion+obj.returnIntJson;
obj.returnIntJson's value is just an integer. obj.returnIntJson的值只是一个整数。 I want to take the value held there, for example: 2043, and add 2043 to the end of qarowquestion.
我想获取那里保存的值,例如:2043,并将2043添加到qarowquestion的末尾。 Thus, I would have:
因此,我将拥有:
var qarowquestion2043;
Thanks. 谢谢。
var myObj = {};
myObj['qarowquestion' + obj.returnIntJson] = 'some Val';
Try the above. 尝试以上。 It will take advantage of dynamic javascript and create a qarowquestion2043 property on the myObj object.
它将利用动态javascript并在myObj对象上创建qarowquestion2043属性。
TGH's reply was almost what I needed, or it is and I just don't know how to use it. TGH的答复几乎是我所需要的,或者只是我所不知道的使用方法。
I changed it so that the part with the number is the value, not the key. 我进行了更改,以便带有数字的部分是值,而不是键。
var myObj = {};
myObj['qarowquestion'] = "qarowquestion" + obj.returnIntJson;
Now, I can access it with: myObj.qarowwquestion
which gives such result: qarowquestion2043
现在,我可以使用以下
myObj.qarowwquestion
访问它: myObj.qarowwquestion
,它给出以下结果: qarowquestion2043
And for a specific example, in my case: 对于一个具体的例子,在我的情况下:
document.getElementById("qarow" + obj.returnIntJson).innerHTML = myObj.qarowquestion;
You cannot create variable, As TGH mentioned you can add your values as a named properties inside an object. 您不能创建变量,正如TGH所述,您可以将值作为对象中的命名属性添加。
var obj = {};
obj['qarowquestion' + obj.returnIntJson] = 'xxx';
By this way you can give proper namespace also for your objects. 这样,您还可以为对象提供适当的名称空间。
var someNameSpace = {};
someNameSpace['someObject'] = 'someVal';
Hope this will be helpful. 希望这会有所帮助。
I was going through a relevant SO post on using dynamic variable names in javascript and the accepted answer brings up a very good point (emphasis is mine): 我正在阅读有关在javascript中使用动态变量名称的相关SO帖子 ,并且可接受的答案提出了一个很好的观点(强调是我的):
Since ECMA-/Javascript is all about Objects and Contexts (which, are also somekind of Object), every variable is stored in a such called Variable- (or in case of a Function, Activation Object).In the Global scope (= NO function context), you implicitly write those variables into the Global object (= window in a browser).
由于ECMA- / Javascript都是关于对象和上下文 (它们也是某种对象)的,因此每个变量都存储在一个称为Variable-(或在函数的情况下是Activation Object)的变量中。函数上下文),则将这些变量隐式地写入Global对象(=浏览器中的窗口)。
So for example (again taken from the answer): 因此,例如(再次从答案中获取):
function foobar() {
this.a = 1;
this.b = 2;
var name = window['a']; // === undefined as it refers to the global context and is undefined
alert(name);
name = this['a']; // === 1
alert(name); // outputs 1
alert(a); //outputs 1
//Since we are talking about context here, you could clearly do
this['qarowquestion'+obj.returnIntJson] = "foo";
alert(qarowquestion2043); //outputs foo
}
So in essence, variables can be accessed as objects in specific contexts which can be really helpful. 因此,从本质上讲,可以将变量作为特定上下文中的对象进行访问,这确实很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.