摘要:對(duì)象解構(gòu)如果使用解析聲明變量,則必須提供初始化程序也就是等號(hào)右側(cè)的值以下語(yǔ)句有語(yǔ)法錯(cuò)誤解構(gòu)賦值表達(dá)式也就是右側(cè)的表達(dá)式如果為或會(huì)導(dǎo)致程序拋出錯(cuò)誤,因?yàn)槿魏螄L試讀取或的屬性的行為都會(huì)觸發(fā)運(yùn)行時(shí)錯(cuò)誤上面代碼是聲明變量同時(shí)賦值相應(yīng)的屬性值那如果已
對(duì)象解構(gòu)
如果使用var、let、const解析聲明變量,則必須提供初始化程序(也就是等號(hào)右側(cè)的值)
以下語(yǔ)句有語(yǔ)法錯(cuò)誤
var { type, name }; let { type, name } const { type, name }
解構(gòu)賦值表達(dá)式(也就是右側(cè)的表達(dá)式)如果為null或undefined會(huì)導(dǎo)致程序拋出錯(cuò)誤,因?yàn)槿魏螄L試讀取null或undefined的屬性的行為都會(huì)觸發(fā)運(yùn)行時(shí)錯(cuò)誤
let node = { type: "Identifier", name: "angela" } let { type, name } = node
上面代碼是聲明type、name變量同時(shí)賦值node相應(yīng)的屬性值
那如果已經(jīng)存在type、name,重新賦值 使用解構(gòu)的話則需要在表達(dá)式兩側(cè)加小括號(hào)
let node = { type: "Identifier", name: "angela" }, type = "demo", name = 1; //添加小括號(hào)可以將塊語(yǔ)句轉(zhuǎn)化為一個(gè)表達(dá)式,從而實(shí)現(xiàn)整個(gè)解構(gòu)賦值的過(guò)程 ({ type, name } = node)
在任何使用值的地方你都可以使用解構(gòu)賦值表達(dá)式
let node = { type: "Identifier", name: "angela" }, type = "demo", name = 1; function outputInfo(value) { console.log(value === node) } outputInfo({ type, name } = node)//true
解構(gòu)還可以使用默認(rèn)值
let node = { type: "Identifier", name: "angela" } let { type, name, value = true } = node
為非同名局部變量賦值
let node = { type: "Identifier" } let { type: localType, name: localName = "angela" } = node console.log(localType)//Identifier console.log(localName)//angela
解構(gòu)嵌套對(duì)象,很可能會(huì)無(wú)意中創(chuàng)建一個(gè)無(wú)效表達(dá)式,比方說(shuō)下面的loc后的大括號(hào)則不需要,更好的做法是定義一個(gè)默認(rèn)值
let { loc: { } } = node數(shù)組解構(gòu)
let colors = ["red", "green", "blue"] let [, , thirdColor] = colors
可以像如上所示只取數(shù)組第三個(gè)元素,忽略前兩個(gè)
let colors = ["red", "green", "blue"], firstColor = "black", secondColor = "purple"; [firstColor, secondColor] = colors
對(duì)變量重新賦值利用解構(gòu)時(shí),數(shù)組解構(gòu)不再需要左右兩側(cè)加小括號(hào)了
可能數(shù)組解構(gòu)用的最多的莫過(guò)于交換值吧
let a = 1, b = 2; [a, b] = [b, a]
同樣數(shù)組解構(gòu)中也可以添加默認(rèn)值
數(shù)組解構(gòu)中有一個(gè)不定元素的概念,可以通過(guò)...語(yǔ)法將數(shù)組中的其余元素賦值給一個(gè)特定的變量
let colors = ["red", "green", "blue"]; let [firstColor, ...restColors] = colors//restColors包含兩個(gè)元素green和blue
concat方法的設(shè)計(jì)初衷是連接兩個(gè)數(shù)組,如果調(diào)用時(shí)不傳遞參數(shù)就會(huì)返回當(dāng)前函數(shù)的副本
let colors = ["red", "green", "blue"]; let cloneColors = colors.concat() //["red", "green", "blue"]
上述代碼用ES6中不定元素也可以實(shí)現(xiàn)該目標(biāo)
let colors = ["red", "green", "blue"]; let [...cloneColors] = colors //["red", "green", "blue"]
需要注意的是在被解構(gòu)的數(shù)組中,不定元素必須為最后一個(gè)條目,在后面繼續(xù)添加逗號(hào)會(huì)導(dǎo)致語(yǔ)法錯(cuò)誤
解構(gòu)參數(shù)
function setCookie(name, value, { secure, path, domain, expires }={}) { } setCookie("type", "js", { secure: true, expires: 6000 })
想的最全面的就是既使用解構(gòu)又使用默認(rèn)值
const setCookieDefaults = { secure: false, path: "/", domain: "example.com", expires: new Date(Date.now() + 360000000) } function setCookie(name, value, { secure = setCookieDefaults.secure, path = setCookieDefaults.path, domain = setCookieDefaults.domain, expires = setCookieDefaults.expires }) { }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/112444.html
摘要:對(duì)象解構(gòu)如果使用解析聲明變量,則必須提供初始化程序也就是等號(hào)右側(cè)的值以下語(yǔ)句有語(yǔ)法錯(cuò)誤解構(gòu)賦值表達(dá)式也就是右側(cè)的表達(dá)式如果為或會(huì)導(dǎo)致程序拋出錯(cuò)誤,因?yàn)槿魏螄L試讀取或的屬性的行為都會(huì)觸發(fā)運(yùn)行時(shí)錯(cuò)誤上面代碼是聲明變量同時(shí)賦值相應(yīng)的屬性值那如果已 對(duì)象解構(gòu) 如果使用var、let、const解析聲明變量,則必須提供初始化程序(也就是等號(hào)右側(cè)的值)以下語(yǔ)句有語(yǔ)法錯(cuò)誤 var { type, n...
摘要:解構(gòu),一種黑魔法解構(gòu)是從對(duì)象中提取出更小元素的過(guò)程。賦值是對(duì)解構(gòu)出來(lái)的元素進(jìn)行重新賦值??偨Y(jié)本章講解了對(duì)象解構(gòu)賦值和數(shù)組解構(gòu)賦值,以及對(duì)象和數(shù)組混合情況下的解構(gòu)賦值操作,最后一個(gè)知識(shí)點(diǎn)是解構(gòu)函數(shù)的參數(shù)。 解構(gòu),一種黑魔法 解構(gòu)是從對(duì)象中提取出更小元素的過(guò)程。賦值是對(duì)解構(gòu)出來(lái)的元素進(jìn)行重新賦值。 下面的代碼你可能無(wú)法在瀏覽器上實(shí)時(shí)測(cè)試,推薦在babel官網(wǎng)在線測(cè)試代碼:在線測(cè)試ES6代碼...
摘要:當(dāng)冒號(hào)右側(cè)存在花括號(hào)時(shí),表示目標(biāo)被嵌套在對(duì)象的更深一層中。在對(duì)象的嵌套解構(gòu)中同樣能為本地變量使用不同的名稱提取數(shù)組解構(gòu)結(jié)構(gòu)賦值基本忽略一些選項(xiàng)重新賦值默認(rèn)值數(shù)組解構(gòu)賦值同樣允許在數(shù)組任意位置指定默認(rèn)值。 主要知識(shí)點(diǎn):對(duì)象解構(gòu)、數(shù)組解構(gòu)、混合解構(gòu)以及參數(shù)解構(gòu)showImg(https://segmentfault.com/img/bVbfWgH?w=1020&h=585); 《深入理解...
閱讀 1701·2021-09-22 15:25
閱讀 1619·2021-09-07 10:06
閱讀 3254·2019-08-30 15:53
閱讀 1152·2019-08-29 13:12
閱讀 3458·2019-08-29 13:07
閱讀 803·2019-08-28 18:19
閱讀 2348·2019-08-27 10:57
閱讀 1042·2019-08-26 13:29