前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >深入理解ES6之—数据解构

深入理解ES6之—数据解构

作者头像
寻找石头鱼
发布2019-08-20 11:12:49
4150
发布2019-08-20 11:12:49
举报
文章被收录于专栏:寻找石头鱼寻找石头鱼

一 对象解构

对象解构语法在赋值语句的左侧使用了对象字面量

代码语言:javascript
复制
let node = {
    type: true,
    name: false
}

//既声明又赋值
let {
    type,
    name
} = node;

//或者先声明再赋值
let type, name
({type,name} = node);
console.log(type);//true
console.log(name);//false

type与name标识符既声明了本地变量,也读取了对象的相应属性值。

解构赋值表达式的值为表达式右侧的值。当解构表达式的右侧的计算结果为null或者undefined时,会抛出错误。

默认值

当你使用解构赋值语句时,如果所指定的本地变量在对象中没有找到同名属性,那么该变量会被赋值为undefined

代码语言:javascript
复制
let node = {
    type: true,
    name: false
},
    type, name, value;
({type,value,name} = node);

console.log(type);//true
console.log(name);//false
console.log(value);//undefined

你可以选择性地定义一个默认值,以便在指定属性不存在时使用该值。

代码语言:javascript
复制
let node = {
        type: true,
        name: false
    },
    type, name, value;
({
    type,
    value = true,
    name
} = node);

console.log(type);//true
console.log(name);//false
console.log(value);//true

赋值给不同的本地变量名

代码语言:javascript
复制
let node = {
    type: true,
    name: false,
    value: "dd"
}
let {
    type: localType,
    name: localName,
    value: localValue = "cc"
} = node;
console.log(localType);
console.log(localName);
console.log(localValue);

type:localType这种语法表示要读取名为type的属性,并把它的值存储在变量localType上。该语法与传统对象字面量的语法相反

嵌套的对象结构

代码语言:javascript
复制
let node = {
type: "Identifier",
name: "foo",
loc: {
    start: {
        line: 1,
        column: 1
    },
    end: {
        line: 1,
        column: 4
    }
}
}

let {
loc: localL,
loc: {
    start: localS,
    end: localE
}
} = node;

console.log(localL);// start: {line: 1,column: 1},end: {line: 1,column: 4}
console.log(localS);//{line: 1,column: 1}
console.log(localE);//{line: 1,column: 4}

当冒号右侧存在花括号时,表示目标被嵌套在对象的更深一层中(loc: {start: localS,end: localE})

二 数据解构

数组解构的语法看起来跟对象解构非常相似,只是将对象字面量换成了数组字面量。

代码语言:javascript
复制
let colors = ["red", "blue", "green"];
let [firstC, secondC, thirdC, thursC = "yellow"] = colors;
console.log(firstC//red
console.log(secondC);//blue
console.log(thirdC);//green
console.log(thursC);//yellow

你也可以在解构模式中忽略一些项,并只给感兴趣的项提供变量名。

代码语言:javascript
复制
let colors = ["red","green","blue"];

let [,,thirdC] = colors;
console.log(thirdC);//blue

thirdC之前的逗号是为数组前面的项提供的占位符。使用这种方法,你就可以轻易从数组任意位置取出值,而无需给其他项提供名称。

解构赋值

代码语言:javascript
复制
let colors = ["red","green","blue"],
    firstColor = "black",
    secondColor = "purple";
[firstColor,secondColor] = colors;
console.log(firstColor);//red
console.log(secondColor);//green

数组解构有一个非常独特的用例,能轻易的互换两个变量的值。

代码语言:javascript
复制
let a =1,b =2;
[a,b] = [b,a];
console.log(a);//2
console.log(b);//1

嵌套的解构

代码语言:javascript
复制
 let colors = ["red", ["green", "blue"], "yellow"];
let [firstC, [, ssc]] = colors;
console.log(ssc);//blue

剩余项

代码语言:javascript
复制
let colors = ["red", "green", "blue"];
let [firstC, ...restC] = colors;
console.log(firstC);
console.log(...restC);
console.log(restC[0]);//green
console.log(restC[1]);//blue

使用剩余项可以进行数组克隆

代码语言:javascript
复制
let colors = ["red", "green", "blue"];
let [...restC] = colors;
console.log(restC);//["red", "green","blue"]

三 混合解构

代码语言:javascript
复制
let node = {
type: "Identifier",
name: 'foo',
loc: {
    start: {
        line: 1,
        column: 1
    },
    end: {
        line: 1,
        column: 4
    }
},
range: [0, 3]
}

let {
type,
name: localName,
loc: {
    start: {
        line: ll
    },
    end: {
        column: col
    }
},
range: [, second]
} = node;

console.log(type);//Identifier
console.log(localName);//foo
console.log(ll);//1
console.log(col);//4
console.log(second);//3
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一 对象解构
    • 默认值
      • 赋值给不同的本地变量名
        • 嵌套的对象结构
        • 二 数据解构
        • 解构赋值
        • 嵌套的解构
        • 剩余项
        • 三 混合解构
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档