esnext.iterator.constructor.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. var $ = require('../internals/export');
  4. var global = require('../internals/global');
  5. var anInstance = require('../internals/an-instance');
  6. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  7. var fails = require('../internals/fails');
  8. var has = require('../internals/has');
  9. var wellKnownSymbol = require('../internals/well-known-symbol');
  10. var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype;
  11. var IS_PURE = require('../internals/is-pure');
  12. var ITERATOR = wellKnownSymbol('iterator');
  13. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  14. var NativeIterator = global.Iterator;
  15. // FF56- have non-standard global helper `Iterator`
  16. var FORCED = IS_PURE
  17. || typeof NativeIterator != 'function'
  18. || NativeIterator.prototype !== IteratorPrototype
  19. // FF44- non-standard `Iterator` passes previous tests
  20. || !fails(function () { NativeIterator({}); });
  21. var IteratorConstructor = function Iterator() {
  22. anInstance(this, IteratorConstructor);
  23. };
  24. if (IS_PURE) {
  25. IteratorPrototype = {};
  26. createNonEnumerableProperty(IteratorPrototype, ITERATOR, function () {
  27. return this;
  28. });
  29. }
  30. if (!has(IteratorPrototype, TO_STRING_TAG)) {
  31. createNonEnumerableProperty(IteratorPrototype, TO_STRING_TAG, 'Iterator');
  32. }
  33. if (FORCED || !has(IteratorPrototype, 'constructor') || IteratorPrototype.constructor === Object) {
  34. createNonEnumerableProperty(IteratorPrototype, 'constructor', IteratorConstructor);
  35. }
  36. IteratorConstructor.prototype = IteratorPrototype;
  37. $({ global: true, forced: FORCED }, {
  38. Iterator: IteratorConstructor
  39. });