将JS变量名传递给函数 [英] Passing JS variable name to function
本文介绍了将JS变量名传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我想要做的:
-
设置了名为
data_1
的变量.
var data_1 = {
"y_legend": {
"text": "# of Patients",
"style": "{font-size: 20px; color: #778877}"
},
"x_legend": {
"text": "AUG 09 - OCT 09",
"style": "{font-size: 20px; color: #778877}"
}
};
在下拉菜单中,用户选择值为data_1
的调用load('data_1')
的选项.
In a drop down a user selects an option with the value of data_1
that calls load('data_1')
.
function load(data)
{
tmp = findSWF("my_chart");
x = tmp.load( JSON.stringify(data) );
}
我的问题:我正在选择一个值为data_1
的选项,而不是变量本身.
因此,在函数load('data_1')
中,当我alert(data)
时,我得到data = 'data_1'
.
My Problem: I'm selecting an option with the value data_1
and not the variable itself.
So in my function load('data_1')
, when I alert(data)
I get data = 'data_1'
.
那么如何通过仅传递字符串名称来获取加载函数中变量data_1
的内容?
So how do I get the contents of my variable data_1
in my load function by passing only the name of the string?
推荐答案
var data_1 = { /* data goes here */ };
var data_choices = {1: data_1, 2: data_2, /* and so on */};
var load = function (data) {
// data is "1", "2", etc. If you want to use the full data_1 name, change
// the data_choices object keys.
var tmp = findSWF("my_chart");
var x = tmp.load( JSON.stringify(data_choices[data]) );
}
这篇关于将JS变量名传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文