es6.array.slice.js 933 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. var $export = require('./_export');
  3. var html = require('./_html');
  4. var cof = require('./_cof');
  5. var toAbsoluteIndex = require('./_to-absolute-index');
  6. var toLength = require('./_to-length');
  7. var arraySlice = [].slice;
  8. // fallback for not array-like ES3 strings and DOM objects
  9. $export($export.P + $export.F * require('./_fails')(function () {
  10. if (html) arraySlice.call(html);
  11. }), 'Array', {
  12. slice: function slice(begin, end) {
  13. var len = toLength(this.length);
  14. var klass = cof(this);
  15. end = end === undefined ? len : end;
  16. if (klass == 'Array') return arraySlice.call(this, begin, end);
  17. var start = toAbsoluteIndex(begin, len);
  18. var upTo = toAbsoluteIndex(end, len);
  19. var size = toLength(upTo - start);
  20. var cloned = new Array(size);
  21. var i = 0;
  22. for (; i < size; i++) cloned[i] = klass == 'String'
  23. ? this.charAt(start + i)
  24. : this[start + i];
  25. return cloned;
  26. }
  27. });