通过文本别名引用Javascript变量 [英] Reference a Javascript variable by a text alias
本文介绍了通过文本别名引用Javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以通过文本别名引用JavaScript变量?例如:
Is it possible to reference a JavaScript variable by a text alias? For example:
var x = 2;
var y = convertToVariableRef("x");
调用上述函数后: y
与 x
的引用相同,而不仅仅是将 x
的值复制到 y
。
After calling the above function:
"y
would be the same reference as x
and not just simply copying the value of x
into y
".
推荐答案
你可以直接创建一个Object反射和分配它的属性。
You could go right on and create an Object reflect and assign properties to it.
var reflect = new Object();
reflect.x = 2;
var y = reflect["x"];
小提琴: http://jsfiddle.net/wE4Ft/
这篇关于通过文本别名引用Javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文