es.array.at.js 733 B

1234567891011121314151617181920
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var toObject = require('../internals/to-object');
  4. var lengthOfArrayLike = require('../internals/length-of-array-like');
  5. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  6. var addToUnscopables = require('../internals/add-to-unscopables');
  7. // `Array.prototype.at` method
  8. // https://tc39.es/ecma262/#sec-array.prototype.at
  9. $({ target: 'Array', proto: true }, {
  10. at: function at(index) {
  11. var O = toObject(this);
  12. var len = lengthOfArrayLike(O);
  13. var relativeIndex = toIntegerOrInfinity(index);
  14. var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex;
  15. return (k < 0 || k >= len) ? undefined : O[k];
  16. }
  17. });
  18. addToUnscopables('at');