array-fill.js 760 B

1234567891011121314151617
  1. 'use strict';
  2. var toObject = require('../internals/to-object');
  3. var toAbsoluteIndex = require('../internals/to-absolute-index');
  4. var lengthOfArrayLike = require('../internals/length-of-array-like');
  5. // `Array.prototype.fill` method implementation
  6. // https://tc39.es/ecma262/#sec-array.prototype.fill
  7. module.exports = function fill(value /* , start = 0, end = @length */) {
  8. var O = toObject(this);
  9. var length = lengthOfArrayLike(O);
  10. var argumentsLength = arguments.length;
  11. var index = toAbsoluteIndex(argumentsLength > 1 ? arguments[1] : undefined, length);
  12. var end = argumentsLength > 2 ? arguments[2] : undefined;
  13. var endPos = end === undefined ? length : toAbsoluteIndex(end, length);
  14. while (endPos > index) O[index++] = value;
  15. return O;
  16. };