Skip to content

Commit 1997b59

Browse files
add indexOf()
1 parent 6151a85 commit 1997b59

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

ieFixer.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,71 @@
99
String.prototype.trim = function () {
1010
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
1111
};
12-
}
13-
})();
12+
}
13+
14+
// Production steps of ECMA-262, Edition 5, 15.4.4.14
15+
// Reference: http://es5.github.io/#x15.4.4.14
16+
if (!Array.prototype.indexOf) {
17+
Array.prototype.indexOf = function(searchElement, fromIndex) {
18+
19+
var k;
20+
21+
// 1. Let O be the result of calling ToObject passing
22+
// the this value as the argument.
23+
if (this == null) {
24+
throw new TypeError('"this" is null or not defined');
25+
}
26+
27+
var O = Object(this);
28+
29+
// 2. Let lenValue be the result of calling the Get
30+
// internal method of O with the argument "length".
31+
// 3. Let len be ToUint32(lenValue).
32+
var len = O.length >>> 0;
33+
34+
// 4. If len is 0, return -1.
35+
if (len === 0) {
36+
return -1;
37+
}
38+
39+
// 5. If argument fromIndex was passed let n be
40+
// ToInteger(fromIndex); else let n be 0.
41+
var n = +fromIndex || 0;
42+
43+
if (Math.abs(n) === Infinity) {
44+
n = 0;
45+
}
46+
47+
// 6. If n >= len, return -1.
48+
if (n >= len) {
49+
return -1;
50+
}
51+
52+
// 7. If n >= 0, then Let k be n.
53+
// 8. Else, n<0, Let k be len - abs(n).
54+
// If k is less than 0, then let k be 0.
55+
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
56+
57+
// 9. Repeat, while k < len
58+
while (k < len) {
59+
// a. Let Pk be ToString(k).
60+
// This is implicit for LHS operands of the in operator
61+
// b. Let kPresent be the result of calling the
62+
// HasProperty internal method of O with argument Pk.
63+
// This step can be combined with c
64+
// c. If kPresent is true, then
65+
// i. Let elementK be the result of calling the Get
66+
// internal method of O with the argument ToString(k).
67+
// ii. Let same be the result of applying the
68+
// Strict Equality Comparison Algorithm to
69+
// searchElement and elementK.
70+
// iii. If same is true, return k.
71+
if (k in O && O[k] === searchElement) {
72+
return k;
73+
}
74+
k++;
75+
}
76+
return -1;
77+
};
78+
}
79+
})();

0 commit comments

Comments
 (0)