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

前端 未结 10 1604
爱一瞬间的悲伤
爱一瞬间的悲伤 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 03:52

    it works for me.

    if (!Array.prototype.indexOf) {
      Array.prototype.indexOf = function(elt /*, from*/) {
        var len = this.length >>> 0;
    
        var from = Number(arguments[1]) || 0;
        from = (from < 0)? Math.ceil(from) : Math.floor(from);
        if (from < 0)
        from += len;
    
        for (; from < len; from++) {
          if (from in this && this[from] === elt)
            return from;
        }
        return -1;
      };
    }
    

提交回复
热议问题