博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[ES6] 08. Destructuring Assignment -- 1
阅读量:5879 次
发布时间:2019-06-19

本文共 2229 字,大约阅读时间需要 7 分钟。

Here is the way you get value from an object:

var obj = {    color: "blue"}console.log(obj.color);  //blue

 

Destructuring Assignment:


 

Object

Destructuring Assignment is somehow different: 

var {color} = {    color: "green"}console.log(color); //green

It tells to looking for color property, so I get "green".

 

If I have muli pros (color, name, position, state): And I want to get color, and position properties.

var {color, position} = {    color: "green",    name: "Great",    position: "Finland",    state: "Who knows"}console.log(color); //greenconsole.log(position); //Finland

 

Function

If I have a fn which return an object: And from the object, I just want name and state.

function getObj(){    return{        color: "green",        name: "Great",        position: "Finland",        state: "Who knows"    };}var {name, state} = getObj();console.log(name); //Greatconsole.log(state); //Who knows

 

From the example, if you want to name something else:

{name: who, state: where}
function getObj(){    return{        color: "green",        name: "Great",        position: "Finland",        state: "Who knows"    };}var {name: who, state: where} = getObj();console.log(who); //Greatconsole.log(where); //Who knows

 

Array

If I have an array, from which I just want the first and the third element from the array:

var [first,,third] = ['first', 'second', 'third', 'forth'];console.log(first);  //firstconsole.log(third);  //third

 

If I want to forEach of array: and get the firstName

var people = [    {        firstName: "Allen",        secondName: "Hook",        email: "allen@abc.com"    },    {        firstName: "Anton",        secondName: "Tee",        email: "tee@abc.com"    },    {        firstName: "Yui",        secondName: "Gegg",        email: "gegg@abc.com"    }];people.forEach(({firstName}) => console.log(firstName));/* Allen Anton Yui * */

 

To make it clear:

people.foreach(function(person){    console.log(person.firstName);});//Destructuring people.foreach(function( {firstName} ){    console.log(firstName);});//Destructuring  + Allowpeople.foreach( ( {firstName} ) => console.log(firstName); )

 

or:

var [, secondPerson] = people;function logEmail({email}){    console.log(email);}logEmail(secondPerson); //tee@abc.com

 

转载地址:http://efdix.baihongyu.com/

你可能感兴趣的文章
CSS魔法堂:Transition就这么好玩
查看>>
【OpenStack】network相关知识学习
查看>>
centos 7下独立的python 2.7环境安装
查看>>
[日常] 算法-单链表的创建
查看>>
前端工程化系列[01]-Bower包管理工具的使用
查看>>
使用 maven 自动将源码打包并发布
查看>>
Spark:求出分组内的TopN
查看>>
Python爬取豆瓣《复仇者联盟3》评论并生成乖萌的格鲁特
查看>>
关于跨DB增量(增、改)同步两张表的数据小技巧
查看>>
学员会诊之03:你那惨不忍睹的三层架构
查看>>
vue-04-组件
查看>>
Golang协程与通道整理
查看>>
解决win7远程桌面连接时发生身份验证错误的方法
查看>>
C/C++ 多线程机制
查看>>
js - object.assign 以及浅、深拷贝
查看>>
python mysql Connect Pool mysql连接池 (201
查看>>
Boost在vs2010下的配置
查看>>
一起谈.NET技术,ASP.NET伪静态的实现及伪静态的意义
查看>>
20款绝佳的HTML5应用程序示例
查看>>
string::c_str()、string::c_data()及string与char *的正确转换
查看>>