SpeciesConstructor.js 844 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $species = GetIntrinsic('%Symbol.species%', true);
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var IsConstructor = require('./IsConstructor');
  6. var Type = require('./Type');
  7. // https://ecma-international.org/ecma-262/6.0/#sec-speciesconstructor
  8. module.exports = function SpeciesConstructor(O, defaultConstructor) {
  9. if (Type(O) !== 'Object') {
  10. throw new $TypeError('Assertion failed: Type(O) is not Object');
  11. }
  12. var C = O.constructor;
  13. if (typeof C === 'undefined') {
  14. return defaultConstructor;
  15. }
  16. if (Type(C) !== 'Object') {
  17. throw new $TypeError('O.constructor is not an Object');
  18. }
  19. var S = $species ? C[$species] : void 0;
  20. if (S == null) {
  21. return defaultConstructor;
  22. }
  23. if (IsConstructor(S)) {
  24. return S;
  25. }
  26. throw new $TypeError('no constructor found');
  27. };