shim.js 916 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var supportsDescriptors = require('define-properties').supportsDescriptors;
  3. var functionsHaveNames = require('functions-have-names')();
  4. var getPolyfill = require('./polyfill');
  5. var defineProperty = Object.defineProperty;
  6. var TypeErr = TypeError;
  7. module.exports = function shimName() {
  8. var polyfill = getPolyfill();
  9. if (functionsHaveNames) {
  10. return polyfill;
  11. }
  12. if (!supportsDescriptors) {
  13. throw new TypeErr('Shimming Function.prototype.name support requires ES5 property descriptor support.');
  14. }
  15. var functionProto = Function.prototype;
  16. defineProperty(functionProto, 'name', {
  17. configurable: true,
  18. enumerable: false,
  19. get: function () {
  20. var name = polyfill.call(this);
  21. if (this !== functionProto) {
  22. defineProperty(this, 'name', {
  23. configurable: true,
  24. enumerable: false,
  25. value: name,
  26. writable: false
  27. });
  28. }
  29. return name;
  30. }
  31. });
  32. return polyfill;
  33. };