Object.defineProperty.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. load("test-common.js");
  2. try {
  3. var o = {};
  4. Object.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false });
  5. assert(o.foo === 1);
  6. o.foo = 2;
  7. assert(o.foo === 1);
  8. var d = Object.getOwnPropertyDescriptor(o, "foo");
  9. assert(d.configurable === false);
  10. assert(d.enumerable === false);
  11. assert(d.writable === false);
  12. assert(d.value === 1);
  13. Object.defineProperty(o, "bar", { value: "hi", writable: true, enumerable: true });
  14. assert(o.bar === "hi");
  15. o.bar = "ho";
  16. assert(o.bar === "ho");
  17. d = Object.getOwnPropertyDescriptor(o, "bar");
  18. assert(d.configurable === false);
  19. assert(d.enumerable === true);
  20. assert(d.writable === true);
  21. assert(d.value === "ho");
  22. assertThrowsError(() => {
  23. Object.defineProperty(o, "bar", { value: "xx", enumerable: false });
  24. }, {
  25. error: TypeError
  26. });
  27. Object.defineProperty(o, "baz", { value: 9, configurable: true, writable: false });
  28. Object.defineProperty(o, "baz", { configurable: true, writable: true });
  29. d = Object.getOwnPropertyDescriptor(o, "baz");
  30. assert(d.configurable === true);
  31. assert(d.writable === true);
  32. assert(d.value === 9);
  33. Object.defineProperty(o, "qux", {
  34. configurable: true,
  35. get() {
  36. return o.secret_qux + 1;
  37. },
  38. set(value) {
  39. this.secret_qux = value + 1;
  40. },
  41. });
  42. o.qux = 10;
  43. assert(o.qux === 12);
  44. o.qux = 20;
  45. assert(o.qux = 22);
  46. Object.defineProperty(o, "qux", { configurable: true, value: 4 });
  47. assert(o.qux === 4);
  48. o.qux = 5;
  49. assert(o.qux = 4);
  50. Object.defineProperty(o, "qux", {
  51. configurable: false,
  52. get() {
  53. return this.secret_qux + 2;
  54. },
  55. set(value) {
  56. o.secret_qux = value + 2;
  57. },
  58. });
  59. o.qux = 10;
  60. assert(o.qux === 14);
  61. o.qux = 20;
  62. assert(o.qux = 24);
  63. assertThrowsError(() => {
  64. Object.defineProperty(o, "qux", {
  65. configurable: false,
  66. get() {
  67. return this.secret_qux + 2;
  68. },
  69. });
  70. }, {
  71. error: TypeError,
  72. message: "Cannot change attributes of non-configurable property 'qux'",
  73. });
  74. assertThrowsError(() => {
  75. Object.defineProperty(o, "qux", { value: 2 });
  76. }, {
  77. error: TypeError,
  78. message: "Cannot change attributes of non-configurable property 'qux'",
  79. });
  80. assertThrowsError(() => {
  81. Object.defineProperty(o, "a", {
  82. get() {},
  83. value: 9,
  84. });
  85. }, {
  86. error: TypeError,
  87. message: "Accessor property descriptors cannot specify a value or writable key",
  88. });
  89. assertThrowsError(() => {
  90. Object.defineProperty(o, "a", {
  91. set() {},
  92. writable: true,
  93. });
  94. }, {
  95. error: TypeError,
  96. message: "Accessor property descriptors cannot specify a value or writable key",
  97. });
  98. console.log("PASS");
  99. } catch (e) {
  100. console.log(e)
  101. }