[TOC]
>[success] # 數組解構
~~~
'對象解構'與'數組結構'有一個重要的不同。'數組的元素是按次序排列的,變量的取值由它的位置決定',
而對象的'屬性'沒有次序,'變量'必須與'屬性同名',才能取到正確的值。
~~~
<br/>
>[success] ## 初始化時解構
~~~
let colors = ["red", "green", "blue"]
let [firstColor, secondColor] = colors
console.log(firstColor) // "red"
console.log(secondColor) // "green"
或者只想初始化時候'取某一個'
let colors = ["red", "green", "blue"]
let [, , thirdColor] = colors
console.log(thirdColor) // "blue"
~~~
<br/>
>[success] ## 賦值時解構
~~~
'數組賦值時解構',與'對象解構'不同,不必將表達式包含在'圓括號'內
~~~
~~~
let colors = ["red", "green", "blue"],
firstColor = "black",
secondColor = "purple";
[firstColor, secondColor] = colors
console.log(firstColor) // "red"
console.log(secondColor) // "green"
~~~
<br/>
>[success] ## 互換變量值
~~~
'數組解構賦值'有一個非常獨特的用例,能輕易地'互換兩個變量的值'。'互換變量值'在排序算法中十分常用,
而在'ES5'中需要使用第'三'個變量作為'臨時變量',正如下例:
~~~
<br/>
>[success] ### ES5互換變量
~~~
// 在 ES5 中互換值
let a = 1,
b = 2,
tmp;
// 賦值
tmp = a
a = b
b = tmp
console.log(a) // 2
console.log(b) // 1
~~~
<br/>
>[success] ### ES6互換變量
~~~
// 在 ES6 中互換值
let a = 1,
b = 2;
// 賦值
[a, b] = [b, a]
console.log(a) // 2
console.log(b) // 1
~~~
<br/>
>[success] ## 默認值
~~~
'數組解構賦值'同樣允許在'數組任意位置'指定'默認值'。當指定位置的項'不存在'、或其值為'undefined',
那么該默認值就會被使用。例如:
~~~
~~~
let colors = ["red"]
let [firstColor, secondColor = "green"] = colors
console.log(firstColor) // "red"
console.log(secondColor) // "green"
~~~
<br/>
>[success] ## 數組嵌套解構
~~~
與'解構嵌套'的'對象'相似,可以用類似的方式來'解構嵌套'的'數組'。在整個'解構模式'中插入另一個
'數組模式',解構操作就會'下行到嵌套的數組中',就像這樣:
~~~
~~~
let colors = ["red", ["green", "lightgreen"], "blue"]
// 隨后
let [firstColor, [secondColor]] = colors
console.log(firstColor); // "red"
console.log(secondColor); // "green"
~~~
<br/>
>[success] ## 剩余項(不定元素)
~~~
let colors = ["red", "green", "blue"]
// 剩余項寫法
let [firstColor, ...restColors] = colors
console.log(firstColor) // "red"
console.log(restColors.length) // 2
console.log(restColors[0]) // "green"
console.log(restColors[1]) // "blue"
~~~
<br/>
>[success] ## 克隆數組
~~~
克隆數組在'JS'中是個明顯被遺漏的功能。在'ES5'中開發者往往使用的是一個簡單的方式,也就是用'concat()'
方法來'克隆數組'
~~~
<br/>
>[success] ## ES5克隆數組
~~~
// 在 ES5 中克隆數組
var colors = [ "red", "green", "blue" ]
var clonedColors = colors.concat()
console.log(clonedColors) // "[red,green,blue]"
~~~
<br/>
>[success] ## ES6克隆數組
~~~
盡管'concat()'方法的本意是'合并'兩個數組,但不使用任何參數來調用此方法,就會獲得'原數組'的一個'克隆品'。
而在'ES6'中,你可以使用''剩余項(...)'的語法來達到同樣效果。實現如下:
~~~
~~~
// 在 ES6 中克隆數組
let colors = ["red", "green", "blue"]
let [...clonedColors] = colors
console.log(clonedColors) // "[red,green,blue]"
~~~
<br/>
>[warning] ## 注意
~~~
'剩余項(...)'必須是數組解構模式中'最后的部分',之后不能再有'逗號',否則就是'語法錯誤'。
~~~
- Javascript基礎篇
- Array數組
- 數組插入值
- filter()
- forEach()
- push()
- pop()
- unshift()
- shift()
- valueOf()
- 面向對象思想
- Javascript 面向對象編程(一):封裝
- Javascript面向對象編程(二):構造函數的繼承
- Javascript面向對象編程(三):非構造函數的繼承
- 解構
- 數組的解構賦值
- 對象的解構賦值
- 函數參數解構
- 字符串的解構賦值
- 數值和布爾值的解構賦值
- 圓括號問題
- 字符串.
- split()
- charAt()
- charCodeAt()
- concat()
- indexOf()
- lastIndexOf()
- match()
- replace()
- includes()
- 初識遞歸
- 渲染ul-li樹形結構
- 異步函數解決方案
- 1. callback回調函數
- 2. ES6 - Promise
- JavaScript高級程序設計(書)
- 在html中使用JavaScript
- script標簽的位置
- 延遲腳本
- 異步腳本
- <noscript>元素
- 基本概念
- 嚴格模式
- 變量詳解
- 數據類型
- typeof操作符
- undefined類型
- Null類型
- Boolean類型
- Number類型
- 深入了解ES6(書)
- var 、let 、 const
- 字符串與正則表達式
- 字符串
- 正則表達式
- 函數
- 函數形參默認值
- 使用不具名參數
- 函數構造器的增強能力
- 擴展運算符
- name屬性
- 明確函數的多重用途
- 塊級函數
- 箭頭函數
- 尾調用優化
- 擴展的對象功能
- 對象類別
- 對象字面量語法的擴展
- ES6對象新增方法
- 重復的對象屬性
- 自有屬性的枚舉順序
- 更強大的原型
- 解構:更方便的數據訪問
- 為什么要用解構?
- 對象解構
- 數組解構
- 混合解構
- 參數解構
- Symbol與Symbol屬性
- 創建Symbol
- Symbol的使用方法
- Symbol全局私有屬性
- Symbol與類型強制轉換
- Symbol屬性檢索
- Symbol的一些構造方法
- Set集合與Map集合
- Set集合
- Weak Set集合(弱引用Set集合)
- Map集合
- JS標準內置對象
- Object 構造函數及屬性
- Object 構造方法
- Symbol 內建對象類的函數及屬性
- Set 構造函數及屬性
- Weak Set 構造函數及屬性
- JS雜項
- 類數組對象
- Class類的理解和使用