JavaScript ES6解構(gòu)運算符的理解和運用
前言
最近一直在學(xué)JavaScript,看到了ES6中的解構(gòu)符號,覺得這個給我們的代碼簡潔性帶來了一個飛躍式的提升,而且它已經(jīng)運用在了企業(yè)開發(fā)中,假如未來你工作中,別人在用,你卻讀不懂別人的代碼,這造成的影響還是很大的。因此,好好學(xué)習(xí)一下吧。
你可以不用,但是你不能不懂✔
JavaScript ES6中,有很多特性都是為了簡化代碼,方便程序員去書寫的。解構(gòu)運算符就是其中很好的特性,它可以通過減少賦值語句的使用,或者減少訪問數(shù)據(jù)下標(biāo)、對象屬性的方式,使得代碼更加簡潔,增強了代碼的可讀性。
解構(gòu)符號的作用
解構(gòu)賦值是對賦值運算符的擴展,他是一種針對數(shù)組或者對象進(jìn)行模式匹配,然后對其中的變量進(jìn)行賦值
ES6 允許按照一定模式,從數(shù)組和對象中提取值,對變量進(jìn)行賦值,這被稱為解構(gòu)
使用方法
基本使用
let [a,b,c] = [1,2,3]; // let a = 1, b = 2, c = 3;
嵌套使用
// 數(shù)組 let [a, [[b], c]] = [1, [[2], 3]]; console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 // 對象 let obj = { x: ['hello', {y: 'world'}] }; let { x: [x,{ y }] } = obj; console.log(x); // hello console.log(y); // world
忽略
// 數(shù)組 let [a, , b] = [1, 2, 3]; console.log(a); // 1 console.log(b); // 3 // 對象 let obj = { x: ['hello', { y: 'world' }] }; let { x: [x, { }] } = obj; console.log(x); // hello
不完全解構(gòu)
// 數(shù)組 let [a = 1, b] = []; console.log(a); // 1 console.log(b); // undefined // 對象 let obj = { x: [{ y: 'world' }] }; let { x: [{ y }, x] } = obj; console.log(x); // undefined console.log(y); // world
剩余運算符
// 數(shù)組 let [a, ...b] = [1, 2, 3]; console.log(a); // 1 console.log(b); // [2,3] // 對象 let {a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40}; console.log(a); // 10 console.log(b); // 20 console.log(rest); // { c: 30, d: 40 }
字符串
let [a, b, c, d, e] = 'hello'; console.log(a); // h console.log(b); // e console.log(c); // l console.log(d); // l console.log(e); // o
解構(gòu)默認(rèn)值
// 當(dāng)解構(gòu)模式有匹配結(jié)果,且匹配結(jié)果是 undefined 時,會觸發(fā)默認(rèn)值作為返回結(jié)果。 let [a = 2] = [undefined]; console.log(a); // 2 // 對象 let {a = 10, b = 5} = {a: 3}; console.log(a); // 3 console.log(b); // 5
交換變量的值.
let a = 1; let b = 2; [a,b] = [b,a]; console.log(a); // 2 console.log(b); // 1
解構(gòu)賦值的應(yīng)用
// 1. 淺克隆與合并 let name = { name: "aaa" } let age = { age: 'bbb' } let person = { ...name, ...age } console.log(person) // { name: "aaa", age: 'bbb' } let a = [1,2,3]; let b = [4,5]; let c = [...a,...b]; console.log(c); // [1,2,3,4,5] // 2. 提取JSON數(shù)據(jù) let JsonData = { id: 10, status: "OK", data: [111, 222] } let { id, status, data: numbers } = JsonData; console.log(id, status, numbers); //10 "OK" [111, 222] // 3. 函數(shù)參數(shù)的定義 // 參數(shù)有序 function fun1([a, b, c]) { console.log(a, b, c) } fun1([1, 2, 3]); // 1 2 3 // 參數(shù)無序 function fun2({ x, y, z }) { console.log(x, y, z) } fun2({ z: 3, x: 2, y: 1 }); // 2 1 3 // 參數(shù)有默認(rèn)值 function fun3 ([a=1,b]) { console.log(a,b); } fun3([,3]) // 1 3
淺談應(yīng)用
提取json數(shù)據(jù)
上面列出了幾種解構(gòu)賦值的應(yīng)用,其中我們最常用的應(yīng)該是第二種,提取json數(shù)據(jù),后端傳給前端的數(shù)據(jù)就是json數(shù)據(jù),前端通常要將數(shù)據(jù)賦值給一個對象,就是使用的這種方法。
可擴展運算符...
我在leetcode上刷題的時候使用過,我是用arr.push(...arr1)來合并兩個數(shù)組的,有點像上面的淺克隆與合并。比起以往我們合并數(shù)組的操作,這個簡直不要太簡單。
第88題,合并兩個有序數(shù)組。
var merge = function(nums1, m, nums2, n) { nums1.length=m; nums2.length=n; nums1.push(...nums2); let arr=nums1.sort((a,b)=>{ return a-b; }) return arr; };
...這個運算符是將數(shù)組中的數(shù)據(jù)遍歷出來,并拷貝到當(dāng)前對象當(dāng)中。
arr.push(...arr1)這個方法會將arr1的數(shù)組元素全部解析出來,然后依次添加到arr中去,完成兩個數(shù)組的合并。
交換變量值
再來看看交換變量值這個應(yīng)用,我依稀記得一位學(xué)長的面試題:不占用額外內(nèi)存空間的情況下,交換a與b的值。當(dāng)時有兩種解法,一是使用異或,二是用數(shù)學(xué)方法,將ab相加,再分別減之,(a=a+b,b=a-b,a=a-b),現(xiàn)在使用解構(gòu)符號的這個方法[a,b] = [b,a],是不是也可以呢?
總結(jié)
到此這篇關(guān)于JavaScript ES6解構(gòu)運算符的理解和運用的文章就介紹到這了,更多相關(guān)ES6解構(gòu)運算符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解JavaScript對Date對象的操作問題(生成一個倒數(shù)7天的數(shù)組)
最近項目需求要生成一個倒數(shù)7天的數(shù)組,下面小編把我的實現(xiàn)思路和代碼整理分享給大家,供大家參考,需要的朋友可以參考下2015-10-10event.keyCode鍵碼值表 附只能輸入特定的字符串代碼
非常不錯的應(yīng)用,讓文本框里只能輸入money大家看下具體的實現(xiàn)代碼,真是只有想到,原理很簡單。2009-05-05JavaScript生成器函數(shù)Generator解決異步操作問題
這篇文章主要為大家介紹了JavaScript生成器函數(shù)Generator解決異步操作問題示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10