我正在尝试使用字符串索引填充字典(JavaScript 对象)并从中检索值(自定义对象/类)。然后,一旦我从字典中获取值,我需要调用这个对象的方法。不幸的是,这不起作用,因为一旦从字典中检索到它,它就只是一个简单的对象(typeof
返回“对象”)。
所以我想做的事情是转换为最初被推入字典的对象类型,在我的例子中是:google.visualization.Gauge
. (参考)
我尝试使用Object.create但这对我不起作用。下面是我的代码:
var _gauges = {};
var methods = {
init: function (options) {
var containers = this;
if (containers != null && containers != undefined && containers.length > 0) {
containers.each(function (index, elem) {
var id = $(elem).attr('id');
var name = $(elem).data('name');
var value = $(elem).data('value');
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
[name, value]
]);
var options = {
width: 400, height: 120,
redFrom: 90, redTo: 100,
yellowFrom: 75, yellowTo: 90,
minorTicks: 5
};
var cont = document.getElementById(id);
var chart = new google.visualization.Gauge(cont);
_gauges[name] = chart;
chart.draw(data, options);
});
}
},
setValue: function (gaugeName, newValue) {
var thisGauge = _gauges[gaugeName];
if (thisGauge) {
thisGauge.setCell(0, 1, newValue);
//var tmp = Object.create(google.visualization.Gauge, thisGauge);
//tmp.setCell(0, 1, newValue);
}
}