array-species-create.js 714 B

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