es6.array.last-index-of.js 964 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. var $export = require('./_export');
  3. var toIObject = require('./_to-iobject');
  4. var toInteger = require('./_to-integer');
  5. var toLength = require('./_to-length');
  6. var $native = [].lastIndexOf;
  7. var NEGATIVE_ZERO = !!$native && 1 / [1].lastIndexOf(1, -0) < 0;
  8. $export($export.P + $export.F * (NEGATIVE_ZERO || !require('./_strict-method')($native)), 'Array', {
  9. // 22.1.3.14 / 15.4.4.15 Array.prototype.lastIndexOf(searchElement [, fromIndex])
  10. lastIndexOf: function lastIndexOf(searchElement /* , fromIndex = @[*-1] */) {
  11. // convert -0 to +0
  12. if (NEGATIVE_ZERO) return $native.apply(this, arguments) || 0;
  13. var O = toIObject(this);
  14. var length = toLength(O.length);
  15. var index = length - 1;
  16. if (arguments.length > 1) index = Math.min(index, toInteger(arguments[1]));
  17. if (index < 0) index = length + index;
  18. for (;index >= 0; index--) if (index in O) if (O[index] === searchElement) return index || 0;
  19. return -1;
  20. }
  21. });