set.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var _Object$defineProperty = require("../core-js/object/define-property");
  2. var _Object$getOwnPropertyDescriptor = require("../core-js/object/get-own-property-descriptor");
  3. var _Reflect$set = require("../core-js/reflect/set");
  4. var getPrototypeOf = require("./getPrototypeOf");
  5. var superPropBase = require("./superPropBase");
  6. var defineProperty = require("./defineProperty");
  7. function set(target, property, value, receiver) {
  8. if (typeof Reflect !== "undefined" && _Reflect$set) {
  9. set = _Reflect$set;
  10. } else {
  11. set = function set(target, property, value, receiver) {
  12. var base = superPropBase(target, property);
  13. var desc;
  14. if (base) {
  15. desc = _Object$getOwnPropertyDescriptor(base, property);
  16. if (desc.set) {
  17. desc.set.call(receiver, value);
  18. return true;
  19. } else if (!desc.writable) {
  20. return false;
  21. }
  22. }
  23. desc = _Object$getOwnPropertyDescriptor(receiver, property);
  24. if (desc) {
  25. if (!desc.writable) {
  26. return false;
  27. }
  28. desc.value = value;
  29. _Object$defineProperty(receiver, property, desc);
  30. } else {
  31. defineProperty(receiver, property, value);
  32. }
  33. return true;
  34. };
  35. }
  36. return set(target, property, value, receiver);
  37. }
  38. function _set(target, property, value, receiver, isStrict) {
  39. var s = set(target, property, value, receiver || target);
  40. if (!s && isStrict) {
  41. throw new Error('failed to set property');
  42. }
  43. return value;
  44. }
  45. module.exports = _set;