【发布时间】:2016-02-19 21:05:58
【问题描述】:
在我的函数/对象中
this.StatusMappings = [
{
Range: [Infinity*-1, 50],
Color: 'red',
Text: 'Just getting started - will require considerable time to complete.'
},
{
Range: [50, 70],
Color: 'yellow',
Text: 'Incomplete - major gaps exist and completion will take some time.'
},
{
Range : [70, 90],
Color : 'yellgreen',
Text : 'Partially completed - but some gaps remain which can be completed shortly.'
},
{
Range: [90, Infinity],
Color: 'green',
Text: 'Fully completed - no additional work needed.'
}
];
this.UpdateInfo = function ( $slider, newval )
{
var color = this.StatusMappings.firstOrUndefined(function (map) {
console.log("map.Range = "); console.log(map.Range);//TEST
return map.Range[0] >= newval && map.Range[1] < newval;
});
$slider.closest('.slider').addClass(color);
}
奇怪的是第一次调用UpdateInfo,一切都按预期进行,而第二次我得到了
未捕获的类型错误:无法读取未定义的属性“0”
因为我的//TEST我第一次看到它有效:
顺便说一下,我的辅助函数firstOrUndefined是
Array.prototype.firstOrUndefined = function ( unpred )
{
for ( var i in this )
if ( unpred(this[i]) )
return this[i];
}
【问题讨论】:
-
我相信这是您的
for-in循环的结果。for-in循环访问属性、方法和数组成员。我的猜测是 this[i] 成员可能是 Array.prototype 实例的属性或方法,而不是实际的 StatusMapping。
标签: javascript jquery