indexof-polyfill.js 1013 Bytes
/**
 * Array indexof polyfill
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/8/8
 * @source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
 */

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(searchElement, fromIndex) { // eslint-disable-line
        var k;
        var o;
        var len;
        var n;

        if (this == null) {  // eslint-disable-line
            throw new TypeError('"this" is null or not defined');
        }

        o = Object(this);

        len = o.length >>> 0;

        if (len === 0) {
            return -1;
        }

        n = +fromIndex || 0;

        if (Math.abs(n) === Infinity) {
            n = 0;
        }

        if (n >= len) {
            return -1;
        }

        k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

        while (k < len) {
            if (k in o && o[k] === searchElement) {
                return k;
            }
            k++;
        }
        return -1;
    };
}