1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| ------------数组解构赋值----------------------------------------------------- let [foo, [[bar], baz]] = [1, [[2]]]; foo bar baz let [ , , third] = ["foo", "bar", "baz"]; third let [x, , y] = [1, 2, 3]; x y let [head, ...tail] = [1, 2, 3, 4]; head tail let [x, y, ...z] = ['a']; x y z let {a = 10, b = 5} = {a: 3};
let {a: aa = 10, b: bb = 5} = {a: 3};
let [x, y] = [1, 2, 3]; x y let [a, b, c, d, e] = 'hello';
let [x = 1] = [null]; x function f() { console.log('aaa'); } let [x = f()] = [1]; let [x = y, y = 1] = []; ----对象解构赋值,变量的取值和赋值次序无关,赋值的属性名必须和变量名一致时赋值。-------------- let { bar, foo ,baz} = { foo: 'aaa', bar: 'bbb' }; foo bar baz let { log, sin, cos } = Math; let { foo: baz } = { foo: 'aaa', bar: 'bbb' }; baz let obj = { p: [ 'Hello', { y: 'World' } ] }; let { p, p: [x, { y }] } = obj; x y p let {foo: {bar}} = {baz: 'baz'}; var { message: msg = 'Something went wrong' } = {}; msg let x; {x} = {x: 1}; ({x} = {x: 1}); let arr = [1, 2, 3]; let {0 : first, [arr.length - 1] : last} = arr; first last const [a, b, c, d, e] = 'hello'; a b let {toString: s} = 123; s === Number.prototype.toString let { prop: x } = undefined; let { prop: y } = null; function add([x, y]){ return x + y; } add([1, 2]); function move({x = 0, y = 0} = {}) { return [x, y]; } move({x: 3, y: 8}); move({x: 3}); move({});
|