1 Arrays and objects can be destructured, both in declarations and in 2 assignment expressions. 3 4 1. Array and object destructuring declarations. 5 6 -- Testcase -- 7 {% 8 let [a, b] = [1, 2]; 9 const { x, y: z, w = 5 } = { x: 10, y: 20 }; 10 let [p, [q, r = 7], , s] = [1, [2], 3, 4]; 11 12 print(a, " ", b, "\n"); 13 print(x, " ", z, " ", w, "\n"); 14 print(p, " ", q, " ", r, " ", s, "\n"); 15 %} 16 -- End -- 17 18 -- Expect stdout -- 19 1 2 20 10 20 5 21 1 2 7 4 22 -- End -- 23 24 25 2. Destructuring assignments evaluate to the assigned value and may target 26 variables, properties and array items. 27 28 -- Testcase -- 29 {% 30 let m, n, o = {}, arr = [1, 2, 3]; 31 32 [m, n] = [3, 4]; 33 [m, n] = [n, m]; 34 print(m, " ", n, "\n"); 35 36 [o.a, o["b"], arr[1]] = [10, 20, 30]; 37 print(o, " ", arr, "\n"); 38 39 ({ m, n: o.k = 99 } = { m: "M" }); 40 print(m, " ", o, "\n"); 41 42 let v = [m, n] = [8, 9]; 43 print(v, " ", m, " ", n, "\n"); 44 45 let i = 0; 46 [arr[i + 1], arr[i]] = [arr[i], arr[i + 1]]; 47 print(arr, "\n"); 48 %} 49 -- End -- 50 51 -- Expect stdout -- 52 4 3 53 { "a": 10, "b": 20 } [ 1, 30, 3 ] 54 M { "a": 10, "b": 20, "k": 99 } 55 [ 8, 9 ] 8 9 56 [ 30, 1, 3 ] 57 -- End -- 58 59 60 3. Targets are evaluated left to right, after the right-hand side. 61 62 -- Testcase -- 63 {% 64 let log = []; 65 let t = []; 66 67 function k(n) { push(log, n); return n; } 68 function src() { push(log, "src"); return ["a", "b"]; } 69 70 [t[k(1)], t[k(2)]] = src(); 71 print(t, " ", log, "\n"); 72 %} 73 -- End -- 74 75 -- Expect stdout -- 76 [ null, "a", "b" ] [ "src", 1, 2 ] 77 -- End -- 78 79 80 4. Default values apply for null items, may refer to preceding items and may 81 be arbitrary expressions, including closures. 82 83 -- Testcase -- 84 {% 85 let [a = 1, b = a + 1] = []; 86 let [c = 1, d = 2] = [null, 0]; 87 let f; 88 89 [f = () => 5] = []; 90 91 print(a, " ", b, " ", c, " ", d, " ", f(), "\n"); 92 %} 93 -- End -- 94 95 -- Expect stdout -- 96 1 2 1 0 5 97 -- End -- 98 99 100 5. Rest elements collect the remaining array items or object properties. 101 102 -- Testcase -- 103 {% 104 let [a, ...r] = [1, 2, 3, 4]; 105 let { b, ...o } = { b: 1, c: 2, d: 3 }; 106 let { x: { y, ...z }, ...w } = { x: { y: 1, q: 2 }, e: 3 }; 107 let [h, ...[i, ...j]] = [1, 2, 3, 4]; 108 let s, t = {}; 109 110 [s, ...t.rest] = [5, 6, 7]; 111 112 print(a, r, "\n", b, o, "\n", y, z, w, "\n", h, i, j, "\n", s, t, "\n"); 113 %} 114 -- End -- 115 116 -- Expect stdout -- 117 1[ 2, 3, 4 ] 118 1{ "c": 2, "d": 3 } 119 1{ "q": 2 }{ "e": 3 } 120 12[ 3, 4 ] 121 5{ "rest": [ 6, 7 ] } 122 -- End -- 123 124 125 6. Computed keys and reserved words as keys. 126 127 -- Testcase -- 128 {% 129 let k = "z"; 130 let { [k]: v, if: x, "a b": y } = { z: 3, if: 1, "a b": 2 }; 131 let a; 132 133 ({ [k]: a } = { z: 4 }); 134 135 print(v, x, y, a, "\n"); 136 %} 137 -- End -- 138 139 -- Expect stdout -- 140 3124 141 -- End -- 142 143 144 7. Declared variables are closed over like any other local. 145 146 -- Testcase -- 147 {% 148 function mk() { 149 let [a, { b, c: [d] }] = [1, { b: 2, c: [3] }]; 150 151 return () => [a, b, d]; 152 } 153 154 let fns = []; 155 156 for (let i = 0; i < 2; i++) { 157 let { x, y = i } = { x: i * 10 }; 158 159 push(fns, () => x + y); 160 } 161 162 print(mk()(), " ", map(fns, f => f()), "\n"); 163 %} 164 -- End -- 165 166 -- Expect stdout -- 167 [ 1, 2, 3 ] [ 0, 11 ] 168 -- End -- 169 170 171 8. Ordinary literals are not affected. 172 173 -- Testcase -- 174 {% 175 let a = 1, b, o = { b: 7 }; 176 177 print([a], [a, a], [a, ], [a = 2], [a + 1], [o.b], { a }, { a: b = 3 }, "\n"); 178 print([[a]], [{ a }], { a: [a] }, "\n"); 179 %} 180 -- End -- 181 182 -- Expect stdout -- 183 [ 1 ][ 1, 1 ][ 1 ][ 2 ][ 3 ][ 7 ]{ "a": 2 }{ "a": 3 } 184 [ [ 2 ] ][ { "a": 2 } ]{ "a": [ 2 ] } 185 -- End -- 186 187 188 9. Pattern-only syntax is rejected in ordinary literals. 189 190 -- Testcase -- 191 {% 192 let a; 193 194 print([, a]); 195 %} 196 -- End -- 197 198 -- Expect stderr -- 199 Syntax error: Expecting expression 200 In line 4, byte 9: 201 202 ` print([, a]);` 203 Near here -^ 204 205 206 -- End -- 207 208 209 10. Patterns must consist of assignment targets only. 210 211 -- Testcase -- 212 {% 213 let a; 214 215 [a + 1] = [2]; 216 %} 217 -- End -- 218 219 -- Expect stderr -- 220 Syntax error: Invalid destructuring target 221 In line 4, byte 3: 222 223 ` [a + 1] = [2];` 224 ~^ 225 226 227 -- End -- 228 229 230 11. Declared variables are not accessible from the initializer. 231 232 -- Testcase -- 233 {% 234 let [a] = a; 235 %} 236 -- End -- 237 238 -- Expect stderr -- 239 Syntax error: Can't access lexical declaration 'a' before initialization 240 In line 2, byte 2: 241 242 ` let [a] = a;` 243 ^-- Near here 244 245 246 -- End -- 247 248 249 12. Destructuring null or non-container values is an error pointing at the 250 pattern. 251 252 -- Testcase -- 253 {% 254 let a; 255 256 [a] = null; 257 %} 258 -- End -- 259 260 -- Expect stderr -- 261 Reference error: left-hand side expression is null 262 In line 4, byte 3: 263 264 ` [a] = null;` 265 ~^~~~~~~~~~ 266 267 268 -- End -- 269 270 271 13. A pattern is an assignment expression and can be used wherever an 272 expression can, evaluating to the assigned value. 273 274 -- Testcase -- 275 {% 276 let a, b, c, v, w; 277 278 v = [a, b] = [1, 2]; 279 [a, b] = [b, a], [a, b] = [a + 10, b + 10]; 280 281 print(v, " ", a, " ", b, "\n"); 282 283 function f(x) { return x; } 284 285 print(f([a, b] = [5, 6]), " ", a, b, "\n"); 286 print([[a] = [7], { c } = { c: 8 }], " ", a, c, "\n"); 287 print(true ? [a] = [9] : 0, " ", a, "\n"); 288 %} 289 -- End -- 290 291 -- Expect stdout -- 292 [ 1, 2 ] 12 11 293 [ 5, 6 ] 56 294 [ [ 7 ], { "c": 8 } ] 78 295 [ 9 ] 9 296 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt