esnext.function.is-callable.js 1.3 KB

1234567891011121314151617181920212223242526272829
  1. var $ = require('../internals/export');
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var $isCallable = require('../internals/is-callable');
  4. var inspectSource = require('../internals/inspect-source');
  5. var hasOwn = require('../internals/has-own-property');
  6. var DESCRIPTORS = require('../internals/descriptors');
  7. // eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
  8. var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
  9. var classRegExp = /^\s*class\b/;
  10. var exec = uncurryThis(classRegExp.exec);
  11. var isClassConstructor = function (argument) {
  12. try {
  13. // `Function#toString` throws on some built-it function in some legacy engines
  14. // (for example, `DOMQuad` and similar in FF41-)
  15. if (!DESCRIPTORS || !exec(classRegExp, inspectSource(argument))) return false;
  16. } catch (error) { /* empty */ }
  17. var prototype = getOwnPropertyDescriptor(argument, 'prototype');
  18. return !!prototype && hasOwn(prototype, 'writable') && !prototype.writable;
  19. };
  20. // `Function.isCallable` method
  21. // https://github.com/caitp/TC39-Proposals/blob/trunk/tc39-reflect-isconstructor-iscallable.md
  22. $({ target: 'Function', stat: true, sham: true, forced: true }, {
  23. isCallable: function isCallable(argument) {
  24. return $isCallable(argument) && !isClassConstructor(argument);
  25. }
  26. });