How to fix Array indexOf() in JavaScript for Internet Explorer browsers

前端 未结 10 1603
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 03:09

If you have worked with JavaScript at any length you are aware that Internet Explorer does not implement the ECMAScript function for Array.prototype.indexOf() [includin

10条回答
  •  轮回少年
    2020-11-22 04:03

    The full code then would be this:

    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(obj, start) {
             for (var i = (start || 0), j = this.length; i < j; i++) {
                 if (this[i] === obj) { return i; }
             }
             return -1;
        }
    }
    

    For a really thorough answer and code to this as well as other array functions check out Stack Overflow question Fixing JavaScript Array functions in Internet Explorer (indexOf, forEach, etc.).

提交回复
热议问题