indexOf.js 584 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Array.indexOf
  3. */
  4. function indexOf(arr, item, fromIndex) {
  5. fromIndex = fromIndex || 0;
  6. if (arr == null) {
  7. return -1;
  8. }
  9. var len = arr.length,
  10. i = fromIndex < 0 ? len + fromIndex : fromIndex;
  11. while (i < len) {
  12. // we iterate over sparse items since there is no way to make it
  13. // work properly on IE 7-8. see #64
  14. if (arr[i] === item) {
  15. return i;
  16. }
  17. i++;
  18. }
  19. return -1;
  20. }
  21. module.exports = indexOf;