具有计算名称的引用变量
Reference variable with computed name
我希望通过使用在运行时动态计算的变量名来访问变量。示例代码:
var firstSecond = ['a', 'b', 'c'];
var tempDatabase = 'first';
var tempTable = 'Second';
var copyArray = MAGIC(tempDatabase + tempTable);
copyArray
应该是数组:['a', 'b', 'c']
。有办法实现吗?
您可以使用一个对象:
var o = {firstSecond: ['a', 'b', 'c']}
var tempDatabase = 'first';
var tempTable = 'Second';
var copyArray = o[tempDatabase + tempTable];
document.write(copyArray)
我希望通过使用在运行时动态计算的变量名来访问变量。示例代码:
var firstSecond = ['a', 'b', 'c'];
var tempDatabase = 'first';
var tempTable = 'Second';
var copyArray = MAGIC(tempDatabase + tempTable);
copyArray
应该是数组:['a', 'b', 'c']
。有办法实现吗?
您可以使用一个对象:
var o = {firstSecond: ['a', 'b', 'c']}
var tempDatabase = 'first';
var tempTable = 'Second';
var copyArray = o[tempDatabase + tempTable];
document.write(copyArray)