为什么我的Javascript在IE8中断?

时间:2015-04-27 10:30:00

标签: javascript jquery internet-explorer-8

以下代码在IE8中断:

getTypes: function (filter) {
    features = []
    _.each(this.collection.models, function (item) {
        _.each(item.get(filter), function (value) {
            if(features.indexOf(value) == -1){ //// error happens here
                features.push(value)
            }
        })
    })
    return features
}

我收到错误消息: 消息:对象不支持此属性或方法

http://jsfiddle.net/w8fm2bf1/

为什么会这样?

2 个答案:

答案 0 :(得分:1)

IE9之前的IE版本不支持.indexOf() function for Array

作为替代方案,您可以使用jQuery.inArray()。像这样:

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(val) {
        return jQuery.inArray(val, this);
    };
}

答案 1 :(得分:0)

在版本9之前,IE中不支持{p> Array.prototype.indexOf。(source)。

您需要对其进行修补(在上面链接的MDN页面上有一个示例)或使用替代方案。

相关问题