object-define-property.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var global = require('../internals/global');
  2. var DESCRIPTORS = require('../internals/descriptors');
  3. var IE8_DOM_DEFINE = require('../internals/ie8-dom-define');
  4. var V8_PROTOTYPE_DEFINE_BUG = require('../internals/v8-prototype-define-bug');
  5. var anObject = require('../internals/an-object');
  6. var toPropertyKey = require('../internals/to-property-key');
  7. var TypeError = global.TypeError;
  8. // eslint-disable-next-line es-x/no-object-defineproperty -- safe
  9. var $defineProperty = Object.defineProperty;
  10. // eslint-disable-next-line es-x/no-object-getownpropertydescriptor -- safe
  11. var $getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
  12. var ENUMERABLE = 'enumerable';
  13. var CONFIGURABLE = 'configurable';
  14. var WRITABLE = 'writable';
  15. // `Object.defineProperty` method
  16. // https://tc39.es/ecma262/#sec-object.defineproperty
  17. exports.f = DESCRIPTORS ? V8_PROTOTYPE_DEFINE_BUG ? function defineProperty(O, P, Attributes) {
  18. anObject(O);
  19. P = toPropertyKey(P);
  20. anObject(Attributes);
  21. if (typeof O === 'function' && P === 'prototype' && 'value' in Attributes && WRITABLE in Attributes && !Attributes[WRITABLE]) {
  22. var current = $getOwnPropertyDescriptor(O, P);
  23. if (current && current[WRITABLE]) {
  24. O[P] = Attributes.value;
  25. Attributes = {
  26. configurable: CONFIGURABLE in Attributes ? Attributes[CONFIGURABLE] : current[CONFIGURABLE],
  27. enumerable: ENUMERABLE in Attributes ? Attributes[ENUMERABLE] : current[ENUMERABLE],
  28. writable: false
  29. };
  30. }
  31. } return $defineProperty(O, P, Attributes);
  32. } : $defineProperty : function defineProperty(O, P, Attributes) {
  33. anObject(O);
  34. P = toPropertyKey(P);
  35. anObject(Attributes);
  36. if (IE8_DOM_DEFINE) try {
  37. return $defineProperty(O, P, Attributes);
  38. } catch (error) { /* empty */ }
  39. if ('get' in Attributes || 'set' in Attributes) throw TypeError('Accessors not supported');
  40. if ('value' in Attributes) O[P] = Attributes.value;
  41. return O;
  42. };