如何通过串联创建另一个变量来访问javascript变量值? [英] How to access javascript variable value by creating another variable via concatenation?

查看:74
本文介绍了如何通过串联创建另一个变量来访问javascript变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下代码 -

Please have a look on the following code -

var abc_text = "Hello";
var def_text = "world";

function testMe(elem) {
    var xyz = elem+"_text";
    alert(xyz);
}

testMe("abc");
testMe("def");

我试图将前缀传递给函数并尝试通过连接打印一些预定义的值。但上面的例子只打印abc_text和def_text..而不是Hello和world。我怎样才能让它发挥作用?

I am trying to pass prefix to a function and the try to print some pre-defined values by concatenating. But the above example just prints "abc_text" and "def_text" .. instead of "Hello" and "world". How can I get it working?

谢谢。

编辑

我正在使用Jquery。

I am using Jquery.

推荐答案

你可以评估xyz,但最好将abc_text和def_text存储在关联数组或对象;

You can eval xyz, but it's better to store abc_text and def_text in associative array or in object;


var text = {"abc" : "Hello", "def" : "Word"};

这篇关于如何通过串联创建另一个变量来访问javascript变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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