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

12345678910111213141516171819202122
  1. 'use strict';
  2. var $export = require('./_export')
  3. , toIObject = require('./_to-iobject')
  4. , toInteger = require('./_to-integer')
  5. , toLength = require('./_to-length')
  6. , $native = [].lastIndexOf
  7. , 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. , length = toLength(O.length)
  15. , 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. });