object-define-property.js 1.8 KB

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