array-species-constructor.js 808 B

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