1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 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 */ 15 16declare function print(arg:any):string; 17function foo({x = 11, y = 22}) 18{ 19 return x + y; 20} 21 22print(foo({})); 23 24// normal 25let [] = []; 26let [c, d] = [1, 2]; 27let [e, , ,...f] = [1, 2, 3, 4, 5, 6]; 28print(c); // 1 29print(d); // 2 30print(e); // 1 31print(f); // 4,5,6 32 33// destructuring more elements 34const foo1 = ["one", "two"]; 35const [red, yellow, green, blue] = foo1; 36 37print(red); // "one" 38print(yellow); // "two" 39print(green); // undefined 40print(blue); //undefined 41 42// swap 43let a = 1; 44let b = 3; 45[a, b] = [b, a]; 46print(a); // 3 47print(b); // 1 48const arr = [1, 2, 3]; 49[arr[2], arr[1]] = [arr[1], arr[2]]; 50print(arr); // [1, 3, 2] 51 52let x, y; 53function fn() { 54 for ([...[x, y]] of [[null]]) { 55 print(x); 56 } 57} 58fn(); 59