本篇內(nèi)容主要講解“JavaScript ES6解構(gòu)運算符運用的方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“JavaScript ES6解構(gòu)運算符運用的方法是什么”吧!
在城區(qū)等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需設(shè)計,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都營銷網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),城區(qū)網(wǎng)站建設(shè)費用合理。
解構(gòu)賦值是對賦值運算符的擴展,他是一種針對數(shù)組或者對象進行模式匹配,然后對其中的變量進行賦值
ES6 允許按照一定模式,從數(shù)組和對象中提取值,對變量進行賦值,這被稱為解構(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)默認值
// 當解構(gòu)模式有匹配結(jié)果,且匹配結(jié)果是 undefined 時,會觸發(fā)默認值作為返回結(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
// 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ù)有默認值 function fun3 ([a=1,b]) { console.log(a,b); } fun3([,3]) // 1 3
上面列出了幾種解構(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ù)遍歷出來,并拷貝到當前對象當中。
arr.push(...arr1)這個方法會將arr1的數(shù)組元素全部解析出來,然后依次添加到arr中去,完成兩個數(shù)組的合并。
再來看看交換變量值這個應(yīng)用,我依稀記得一位學(xué)長的面試題:不占用額外內(nèi)存空間的情況下,交換a與b的值。當時有兩種解法,一是使用異或,二是用數(shù)學(xué)方法,將ab相加,再分別減之,(a=a+b,b=a-b,a=a-b),現(xiàn)在使用解構(gòu)符號的這個方法[a,b] = [b,a],是不是也可以呢?
到此,相信大家對“JavaScript ES6解構(gòu)運算符運用的方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當前名稱:JavaScriptES6解構(gòu)運算符運用的方法是什么
網(wǎng)址分享:http://chinadenli.net/article20/gojojo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、定制網(wǎng)站、網(wǎng)站營銷、品牌網(wǎng)站制作、微信小程序、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)