【发布时间】:2012-06-01 14:23:15
【问题描述】:
我正在尝试填充字典(JavaScript 对象)并使用字符串索引从中检索值。出于某种原因,当我尝试检索值时,它总是返回 undefined。
我的代码是这样的:
var _gauges = {};
//fill the gauges
_gauges[gaugeName] = gaugeObject;
然后我尝试按如下方式访问它:
setValue: function (gaugeName, newValue) {
var thisGauge = _gauges[gaugeName]; //always undefined
console.log(_gauges); //output shows all the elements that were added to _gauges
if (thisGauge) {
thisGauge.setCell(0, 1, newValue);
}
}
我在这里做错了吗?
【问题讨论】:
-
这两个实例中 gaugeName 的值是多少?
-
这是一个简单的字符串值...在这种情况下是“Gauge-0”
-
你确定它在这两种情况下都等于
Gauge-0吗?您是否尝试过记录其值? -
好吧,这是一个愚蠢的错字!感谢您指出@RyanLynch
-
继续并发布一个答案,解释您的问题的解决方案是什么,并接受它以结束此问题。
标签: javascript jquery jquery-plugins javascript-objects