我需要帮助理解为什么以下代码返回未定义的对象属性:
var count = 0;
var intervals = {
    collection : []                      
}
intervals.collection[0] = function () {
    this.timer = setInterval(function(){
       count++;
       $("p").html(count);            
    }, 1000);
}();
if(typeof intervals.collection[0] === "undefined") {
    $("span").html("undefined");        
}
工作示例:http: //jsfiddle.net/tvaQk/8/
基本上,我希望能够保留一组 setIntervals 供以后参考,以便循环并清除。我在想我可以遍历 interval.collection 数组并执行以下操作:
clearInterval(intervals.collection[0].timer)
但不能因为intervals.collection[0]未定义