array-species-constructor.js 759 B

12345678910111213141516171819202122
  1. var isArray = require('../internals/is-array');
  2. var isConstructor = require('../internals/is-constructor');
  3. var isObject = require('../internals/is-object');
  4. var wellKnownSymbol = require('../internals/well-known-symbol');
  5. var SPECIES = wellKnownSymbol('species');
  6. var $Array = Array;
  7. // a part of `ArraySpeciesCreate` abstract operation
  8. // https://tc39.es/ecma262/#sec-arrayspeciescreate
  9. module.exports = function (originalArray) {
  10. var C;
  11. if (isArray(originalArray)) {
  12. C = originalArray.constructor;
  13. // cross-realm fallback
  14. if (isConstructor(C) && (C === $Array || isArray(C.prototype))) C = undefined;
  15. else if (isObject(C)) {
  16. C = C[SPECIES];
  17. if (C === null) C = undefined;
  18. }
  19. } return C === undefined ? $Array : C;
  20. };