1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15var obj = {}; 16Object.defineProperties(obj, { 17 "foo": { 18 value: true, 19 writable: true 20 }, 21 "bar": { 22 value: "baz", 23 writable: false 24 }, 25 "Hello": { 26 value: "world", 27 writable: false 28 }, 29 "inner_object": { 30 value : { 31 "a" : 1, 32 "b" : { 33 value: "foo" 34 } 35 } 36 } 37}); 38 39assert (obj.foo === true); 40assert (obj.bar === "baz"); 41assert (obj.Hello === "world"); 42assert (obj.inner_object.a === 1); 43assert (obj.inner_object.b.value === "foo"); 44 45// These cases should throw TypeError 46try { 47 Object.defineProperties(obj, undefined); 48 assert (false); 49} catch (e) { 50 assert (e instanceof TypeError); 51} 52 53try { 54 Object.defineProperties(obj, null); 55 assert (false); 56} catch (e) { 57 assert (e instanceof TypeError); 58} 59 60try { 61 Object.defineProperties(undefined, { 62 "foo": { 63 value: true, 64 writable: true 65 } 66 }); 67 assert (false); 68} catch (e) { 69 assert (e instanceof TypeError); 70} 71 72// Check for internal assert, see issue #131. 73try { 74 Object.defineProperties([], undefined); 75 assert (false); 76} catch (e) { 77 assert (e instanceof TypeError); 78} 79 80// If one of the properties is wrong than it shouldn't update the object. 81var obj2 = { 82 a: 5 83}; 84try { 85 Object.defineProperties(obj2, { 86 "foo": { 87 value: true, 88 writable: true 89 }, 90 "bar": { 91 value: 3, 92 set: 3 93 }, 94 "Hello": { 95 value: "world", 96 writable: false 97 } 98 }); 99 assert (false); 100} catch (e) { 101 assert (e instanceof TypeError); 102 assert (obj2.foo === undefined); 103 assert (obj2.set === undefined); 104 assert (obj2.Hello === undefined); 105 assert (obj2.a === 5); 106} 107 108// Define accessors 109var obj = {}; 110Object.defineProperties(obj, { 111 "foo": { 112 value: 42, 113 writable: true, 114 }, 115 "bar": { 116 get: function() { return this.foo }, 117 set: function(v) { this.foo = v } 118 } 119}); 120 121assert (obj.bar === 42); 122obj.bar = "baz"; 123assert (obj.foo === "baz"); 124 125// Define get method which throws error 126var obj = {}; 127var props = { 128 prop1: { 129 value: 1, 130 writable: true, 131 }, 132 get bar() { 133 throw new TypeError("foo"); 134 return { value : 2, writable : true }; 135 }, 136 prop2: { 137 value: 3, 138 writable: true, 139 }, 140 prop3: { 141 value: 4, 142 writable: true, 143 } 144}; 145 146try { 147 Object.defineProperties(obj, props); 148 assert (false); 149} catch (e) { 150 assert (e instanceof TypeError); 151 assert (e.message === "foo"); 152} 153 154// Define get method which deletes a property 155var obj = {}; 156Object.defineProperties(obj, { 157 "foo": { 158 value: 42, 159 writable: true, 160 }, 161 "a": { 162 value: "b", 163 configurable: true 164 }, 165 "bar": { 166 get: function() { 167 delete this.a; 168 return this.foo; 169 }, 170 } 171}); 172 173assert (obj.a === "b"); 174assert (obj.bar === 42); 175assert (obj.a === undefined); 176 177// This code should throw TypeError 178var obj = {}; 179var props = { 180 prop1: { 181 value: 1, 182 writable: true, 183 }, 184 get bar() { 185 delete props.prop1; 186 delete props.prop2; 187 return { value : 2, writable : true }; 188 }, 189 prop2: { 190 value: 3, 191 writable: true, 192 }, 193 prop3: { 194 value: 4, 195 writable: true, 196 } 197}; 198 199try { 200 Object.defineProperties(obj, props); 201 assert (false); 202} catch (e) { 203 assert (e instanceof TypeError); 204} 205