双字符串变量名(JavaScript) [英] Double String Variable Name (Javascript)

查看:70
本文介绍了双字符串变量名(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道还有其他与此类似的问题,但是我觉得他们好像没有回答我的问题.我基本上想做动态变量名.让我举一个例子:

I know there are other questions similar to this, but I feel as if they don't answer my question. I would basically like to make Dynamic Variable Names. Let me give you an example:

//Take the strings "new" and "variable" and make them into a variable.
"new" + "variable" = "Hello World";
//So technically the variable name is "newvariable" and has the value "Hello World"

因此,基本上,它需要两个字符串,并将它们组合为一个变量名.我将如何去做?

So basically it takes two strings, and combines them into one variable name. How would I go about doing this?

P.S.这是,可以将变量的值(仅是名称)组合在一起

P.S. This is NOT to combine the values of variables, just the names

推荐答案

将其写入数组或对象

var arr = {};
arr['new' + 'variable'] = 'Hello World';

然后

alert(arr['newvariable']);

alert(arr.newvariable);

这篇关于双字符串变量名(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆