合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# ES6 解构赋值 解构赋值是对赋值运算符的扩展。 他是一种针对数组或者对象进行模式匹配,然后对其中的变量进行赋值。 在代码书写上简洁且易读,语义更加清晰明了;也方便了复杂对象中数据字段获取。 ``` <pre class="calibre13">``` <span class="token2">let</span> <span class="token1">[</span>a<span class="token1">,</span> b<span class="token1">,</span> c<span class="token1">]</span> <span class="token">=</span> <span class="token1">[</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span>\<span class="token1">]</span><span class="token1">;</span> <span class="token6">// a = 1 </span> <span class="token6">// b = 2 </span> <span class="token6">// c = 3</span> <span class="token2">let</span> <span class="token1">[</span>a<span class="token1">,</span> b<span class="token1">,</span> c<span class="token1">,</span> d<span class="token1">,</span> e<span class="token1">]</span> <span class="token">=</span> <span class="token4">'hello'</span><span class="token1">;</span> <span class="token6">// a = 'h' </span> <span class="token6">// b = 'e'</span> <span class="token6">// c = 'l' </span> <span class="token6">// d = 'l' </span> <span class="token6">// e = 'o'</span> ``` ``` 当解构模式有匹配结果,且匹配结果是 undefined 时,会触发默认值作为返回结果。 ``` <pre class="calibre14">``` <span class="token2">let</span> <span class="token1">[</span>a <span class="token">=</span> <span class="token5">2</span><span class="token1">]</span> <span class="token">=</span> <span class="token1">[</span>undefined<span class="token1">]</span><span class="token1">;</span> <span class="token6">// a = 2</span> ``` ```