es.function.name.js 680 B

12345678910111213141516171819202122
  1. var DESCRIPTORS = require('../internals/descriptors');
  2. var defineProperty = require('../internals/object-define-property').f;
  3. var FunctionPrototype = Function.prototype;
  4. var FunctionPrototypeToString = FunctionPrototype.toString;
  5. var nameRE = /^\s*function ([^ (]*)/;
  6. var NAME = 'name';
  7. // Function instances `.name` property
  8. // https://tc39.github.io/ecma262/#sec-function-instances-name
  9. if (DESCRIPTORS && !(NAME in FunctionPrototype)) {
  10. defineProperty(FunctionPrototype, NAME, {
  11. configurable: true,
  12. get: function () {
  13. try {
  14. return FunctionPrototypeToString.call(this).match(nameRE)[1];
  15. } catch (error) {
  16. return '';
  17. }
  18. }
  19. });
  20. }