es6-变量的解构赋值
生活随笔
收集整理的這篇文章主要介紹了
es6-变量的解构赋值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從數組和對象中提取值,對變量進行賦值,這被稱為解構
let [foo, [[bar], baz]] = [1, [[2], 3]]; foo // 1 bar // 2 baz // 3 let [ , , third] = ["foo", "bar", "baz"]; third // "baz" let [x, , y] = [1, 2, 3]; x // 1 y // 3 let [head, ...tail] = [1, 2, 3, 4]; head // 1 tail // [2, 3, 4] let [x, y, ...z] = ['a']; x // "a" y // undefined // 如果解構不成功,變量的值就等于undefined z // [] function* fibs() {let a = 0;let b = 1;while (true) {yield a;[a, b] = [b, a + b];} }let [first, second, third, fourth, fifth, sixth] = fibs(); // 0,1,1,2,3,5 let [x, y, z] = new Set(['a', 'b', 'c']); // set,不重復的數組 x // "a"允許指定默認值:? 當一個數組成員嚴格等于undefined,默認值才會生效
let [foo = true] = []; // foo // true let [x, y = 'b'] = ['a']; // x='a', y='b' let [x, y = 'b'] = ['a', undefined]; // x='a', y='b' let [x = 1] = [undefined]; // x // 1 let [x = 1] = [null]; //x // null?
轉載于:https://www.cnblogs.com/avidya/p/10636664.html
總結
以上是生活随笔為你收集整理的es6-变量的解构赋值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Content-Disposition
- 下一篇: MySQL的存储函数与存储过程的区别