indexof-polyfill.js
1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 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;
};
}