เหตุใด Javascript ของฉันจึงพังใน IE8

รหัสต่อไปนี้แตกใน 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/

ทำไมเป็นเช่นนี้?


person michaelmcgurk    schedule 27.04.2015    source แหล่งที่มา


คำตอบ (2)


IE เวอร์ชันก่อน IE9 ไม่รองรับ .indexOf( ) ฟังก์ชันสำหรับอาร์เรย์

อีกทางเลือกหนึ่ง คุณสามารถใช้ jQuery.inArray() ได้ บางสิ่งเช่นนี้:

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(val) {
        return jQuery.inArray(val, this);
    };
}
person Rahul Tripathi    schedule 27.04.2015
comment
ขอบคุณมากที่สละเวลาอ่านและตอบ คุณช่วยยืนยันได้ไหมว่าฉันควรใช้สิ่งนี้ if(features.inArray(value) == -1){ features.push(value) } ? - person michaelmcgurk; 27.04.2015
comment
@michaelmcgurk:- ลองดูสิ! - person Rahul Tripathi; 27.04.2015
comment
สมบูรณ์แบบ. ขอบคุณ @ราหุล! :-D - person michaelmcgurk; 27.04.2015

Array.prototype.indexOf ไม่รองรับ IE จนถึงเวอร์ชัน 9 (แหล่งที่มา)

คุณจะต้องแก้ไขลิง (มีตัวอย่างในหน้า MDN ที่ลิงก์ด้านบน) หรือใช้ทางเลือกอื่น

person Quentin    schedule 27.04.2015
comment
OP โปรดทราบว่ามีโพลีฟิลที่ด้านล่างของหน้านั้น ซึ่งคุณสามารถใช้กับเบราว์เซอร์ที่ไม่รองรับได้ - person Andy; 27.04.2015