set.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import superPropBase from "./superPropBase.js";
  2. import defineProperty from "./defineProperty.js";
  3. function set(target, property, value, receiver) {
  4. if (typeof Reflect !== "undefined" && Reflect.set) {
  5. set = Reflect.set;
  6. } else {
  7. set = function set(target, property, value, receiver) {
  8. var base = superPropBase(target, property);
  9. var desc;
  10. if (base) {
  11. desc = Object.getOwnPropertyDescriptor(base, property);
  12. if (desc.set) {
  13. desc.set.call(receiver, value);
  14. return true;
  15. } else if (!desc.writable) {
  16. return false;
  17. }
  18. }
  19. desc = Object.getOwnPropertyDescriptor(receiver, property);
  20. if (desc) {
  21. if (!desc.writable) {
  22. return false;
  23. }
  24. desc.value = value;
  25. Object.defineProperty(receiver, property, desc);
  26. } else {
  27. defineProperty(receiver, property, value);
  28. }
  29. return true;
  30. };
  31. }
  32. return set(target, property, value, receiver);
  33. }
  34. export default function _set(target, property, value, receiver, isStrict) {
  35. var s = set(target, property, value, receiver || target);
  36. if (!s && isStrict) {
  37. throw new Error('failed to set property');
  38. }
  39. return value;
  40. }