es6.array.iterator.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. var addToUnscopables = require('./_add-to-unscopables');
  3. var step = require('./_iter-step');
  4. var Iterators = require('./_iterators');
  5. var toIObject = require('./_to-iobject');
  6. // 22.1.3.4 Array.prototype.entries()
  7. // 22.1.3.13 Array.prototype.keys()
  8. // 22.1.3.29 Array.prototype.values()
  9. // 22.1.3.30 Array.prototype[@@iterator]()
  10. module.exports = require('./_iter-define')(Array, 'Array', function (iterated, kind) {
  11. this._t = toIObject(iterated); // target
  12. this._i = 0; // next index
  13. this._k = kind; // kind
  14. // 22.1.5.2.1 %ArrayIteratorPrototype%.next()
  15. }, function () {
  16. var O = this._t;
  17. var kind = this._k;
  18. var index = this._i++;
  19. if (!O || index >= O.length) {
  20. this._t = undefined;
  21. return step(1);
  22. }
  23. if (kind == 'keys') return step(0, index);
  24. if (kind == 'values') return step(0, O[index]);
  25. return step(0, [index, O[index]]);
  26. }, 'values');
  27. // argumentsList[@@iterator] is %ArrayProto_values% (9.4.4.6, 9.4.4.7)
  28. Iterators.Arguments = Iterators.Array;
  29. addToUnscopables('keys');
  30. addToUnscopables('values');
  31. addToUnscopables('entries');