ES6_解构赋值_note
- 解构赋值非常有用
- 例如:
-
<script > let [a, b, c] = [12, 5, 6]; script>
-
- json:
-
<script> let json = { name: "Strive", age: 18, job: "码农" }; let {name, age, job} = json; console.log(name); script>
-
- json中起别名:
-
<script> let json = { name: "Strive", age: 18, job: "码农" } //此处的job:a为起别名 let {name, age, job: a} = json; console.log(name, age, a); script>
-
- 赋予默认值:
-
此时输出的是:<script> // 赋予默认值 // 此时输出undefine时为空值,若输出null,并非空值(可能是后台给的默认值为null) let [a, b, c = "暂无数据"] = ['aa', 'bb']; console.log(a, b, c); script>
-
- 例如: