IsConstructor.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var GetIntrinsic = require('../GetIntrinsic.js');
  3. var $construct = GetIntrinsic('%Reflect.construct%', true);
  4. var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
  5. try {
  6. DefinePropertyOrThrow({}, '', { '[[Get]]': function () {} });
  7. } catch (e) {
  8. // Accessor properties aren't supported
  9. DefinePropertyOrThrow = null;
  10. }
  11. // https://262.ecma-international.org/6.0/#sec-isconstructor
  12. if (DefinePropertyOrThrow && $construct) {
  13. var isConstructorMarker = {};
  14. var badArrayLike = {};
  15. DefinePropertyOrThrow(badArrayLike, 'length', {
  16. '[[Get]]': function () {
  17. throw isConstructorMarker;
  18. },
  19. '[[Enumerable]]': true
  20. });
  21. module.exports = function IsConstructor(argument) {
  22. try {
  23. // `Reflect.construct` invokes `IsConstructor(target)` before `Get(args, 'length')`:
  24. $construct(argument, badArrayLike);
  25. } catch (err) {
  26. return err === isConstructorMarker;
  27. }
  28. };
  29. } else {
  30. module.exports = function IsConstructor(argument) {
  31. // unfortunately there's no way to truly check this without try/catch `new argument` in old environments
  32. return typeof argument === 'function' && !!argument.prototype;
  33. };
  34. }