es.function.name.js 886 B

12345678910111213141516171819202122232425
  1. var DESCRIPTORS = require('../internals/descriptors');
  2. var FUNCTION_NAME_EXISTS = require('../internals/function-name').EXISTS;
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var defineProperty = require('../internals/object-define-property').f;
  5. var FunctionPrototype = Function.prototype;
  6. var functionToString = uncurryThis(FunctionPrototype.toString);
  7. var nameRE = /function\b(?:\s|\/\*[\S\s]*?\*\/|\/\/[^\n\r]*[\n\r]+)*([^\s(/]*)/;
  8. var regExpExec = uncurryThis(nameRE.exec);
  9. var NAME = 'name';
  10. // Function instances `.name` property
  11. // https://tc39.es/ecma262/#sec-function-instances-name
  12. if (DESCRIPTORS && !FUNCTION_NAME_EXISTS) {
  13. defineProperty(FunctionPrototype, NAME, {
  14. configurable: true,
  15. get: function () {
  16. try {
  17. return regExpExec(nameRE, functionToString(this))[1];
  18. } catch (error) {
  19. return '';
  20. }
  21. }
  22. });
  23. }