non-extensible-elements6.js (7307B)
1 load(libdir + "asserts.js"); 2 3 function testNonExtensible() { 4 var a = [1, 2, 3, , 5]; 5 Object.preventExtensions(a); 6 7 // Can change the value. 8 Object.defineProperty(a, 1, {value:7, enumerable: true, configurable: true, writable: true}); 9 assertEq(JSON.stringify(Object.getOwnPropertyDescriptor(a, 1)), 10 `{"value":7,"writable":true,"enumerable":true,"configurable":true}`); 11 12 // Can make non-writable, non-configurable, non-enumerable. 13 Object.defineProperty(a, 1, {value:9, enumerable: true, configurable: true, writable: false}); 14 assertEq(JSON.stringify(Object.getOwnPropertyDescriptor(a, 1)), 15 `{"value":9,"writable":false,"enumerable":true,"configurable":true}`); 16 Object.defineProperty(a, 0, {value:4, enumerable: true, configurable: false, writable: true}); 17 assertEq(JSON.stringify(Object.getOwnPropertyDescriptor(a, 0)), 18 `{"value":4,"writable":true,"enumerable":true,"configurable":false}`); 19 Object.defineProperty(a, 2, {value:3, enumerable: false, configurable: true, writable: true}); 20 assertEq(JSON.stringify(Object.getOwnPropertyDescriptor(a, 2)), 21 `{"value":3,"writable":true,"enumerable":false,"configurable":true}`); 22 23 // Turn into an accessor. 24 Object.defineProperty(a, 4, {get:() => -2, enumerable: true, configurable: true}); 25 26 // Can't add new properties. 27 assertThrowsInstanceOf(() => Object.defineProperty(a, 3, 28 {value:4, enumerable: true, 29 configurable: true, 30 writable: true}), 31 TypeError); 32 assertThrowsInstanceOf(() => Object.defineProperty(a, 10, 33 {value:4, 34 enumerable: true, 35 configurable: true, 36 writable: true}), 37 TypeError); 38 39 assertEq(a.toString(), "4,9,3,,-2"); 40 } 41 for (var i = 0; i < 15; i++) 42 testNonExtensible(); 43 44 function testSealed() { 45 var a = [1, 2, 3, , 5]; 46 Object.seal(a); 47 48 // Can change the value. 49 Object.defineProperty(a, 1, {value:7, enumerable: true, configurable: false, writable: true}); 50 assertEq(JSON.stringify(Object.getOwnPropertyDescriptor(a, 1)), 51 `{"value":7,"writable":true,"enumerable":true,"configurable":false}`); 52 53 // Can make non-writable. 54 Object.defineProperty(a, 0, {value:4, enumerable: true, configurable: false, writable: false}); 55 56 // Can't make configurable, non-enumerable. 57 assertThrowsInstanceOf(() => Object.defineProperty(a, 2, 58 {value:7, 59 enumerable: true, 60 configurable: true, 61 writable: true}), 62 TypeError); 63 assertThrowsInstanceOf(() => Object.defineProperty(a, 2, 64 {value:7, 65 enumerable: false, 66 configurable: false, 67 writable: true}), 68 TypeError); 69 70 // Can't turn into an accessor. 71 assertThrowsInstanceOf(() => Object.defineProperty(a, 4, {get:() => -2, 72 enumerable: true, 73 configurable: true}), 74 TypeError); 75 76 // Can't add new properties. 77 assertThrowsInstanceOf(() => Object.defineProperty(a, 3, 78 {value:4, enumerable: true, 79 configurable: true, 80 writable: true}), 81 TypeError); 82 assertThrowsInstanceOf(() => Object.defineProperty(a, 10, 83 {value:4, 84 enumerable: true, 85 configurable: true, 86 writable: true}), 87 TypeError); 88 89 assertEq(a.toString(), "4,7,3,,5"); 90 } 91 for (var i = 0; i < 15; i++) 92 testSealed(); 93 94 function testFrozen() { 95 var a = [1, 2, 3, , 5]; 96 Object.freeze(a); 97 98 // Can redefine with same value/attributes. 99 Object.defineProperty(a, 0, {value:1, enumerable: true, configurable: false, writable: false}); 100 101 // Can't change the value. 102 assertThrowsInstanceOf(() => Object.defineProperty(a, 1, 103 {value:7, 104 enumerable: true, 105 configurable: false, 106 writable: false}), 107 TypeError); 108 109 // Can't make configurable, non-enumerable. 110 assertThrowsInstanceOf(() => Object.defineProperty(a, 2, 111 {value:3, 112 enumerable: true, 113 configurable: true, 114 writable: false}), 115 TypeError); 116 assertThrowsInstanceOf(() => Object.defineProperty(a, 2, 117 {value:3, 118 enumerable: false, 119 configurable: false, 120 writable: false}), 121 TypeError); 122 // Can't turn into an accessor. 123 assertThrowsInstanceOf(() => Object.defineProperty(a, 4, {get:() => -2, 124 enumerable: true, 125 configurable: true}), 126 TypeError); 127 128 // Can't add new properties. 129 assertThrowsInstanceOf(() => Object.defineProperty(a, 3, 130 {value:4, enumerable: true, 131 configurable: true, 132 writable: true}), 133 TypeError); 134 assertThrowsInstanceOf(() => Object.defineProperty(a, 10, 135 {value:4, 136 enumerable: true, 137 configurable: true, 138 writable: true}), 139 TypeError); 140 141 assertEq(a.toString(), "1,2,3,,5"); 142 } 143 for (var i = 0; i < 15; i++) 144 testFrozen();