Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of es6 deconstruction

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容主要讲解"es6解构的概念是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"es6解构的概念是什么"吧!

在es6中,解构是按照一定模式从数组和对象中提取值,对变量进行赋值的过程;它是一种打破数据结果,将其拆分为更小部分的过程,可以达到简化提取信息的目的;比较常见的有对象解构、数组解构和混合解构。

本教程操作环境:windows10系统、ECMAScript 6.0版、Dell G3电脑。

什么是es6的解构

destructuring:百度百科的意思是结构分解,ES6 中允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring)。

开发中比较常见的有对象解构、 数组解构、混合解构。这是一种将数据结构分解为更小的部分的过程,从而达到简化提取信息的目的。

逐个拆分现有的对象或数组,来提取你所需要的数据。是一种打破数据结构,将其拆分为更小部分的过程

ES6使用了一种新模式来匹配你想要提取的数值, 解构赋值就是采用了这种模式。 该模式会映射出你正在解构的数据结构,只有那些与该模式相匹配的数据,才会被提取出来。

示例对象解构

传统方法获取对象中的值

let node = { type: 'Identifier', name: 'foo'}console.log(node.type) // Identifierconsole.log(node.foo) // foo

使用解构

let node = { type: 'Identifier', name: 'foo'}let { type, name } = nodeconsole.log(type) // Identifierconsole.log(name) // foo

如果指定的局部变量名称在对象中不存在,那么这个局部变量会被赋值为undefined

let { type, name, value } = nodeconsole.log(type) // Identifierconsole.log(name) // fooconsole.log(value) // undefined

当指定的属性不存在时,可以给不存在的属性定义任意的默认值

let { type, name, value = true } = nodeconsole.log(type) // Identifierconsole.log(name) // fooconsole.log(value) // true

指定新的变量名进行解构赋值

let arr = { six: '男', age: 19}let {six:newSix, age:newAge} = arrconsole.log(six, age) // six is not definedconsole.log(newSix, newAge) // 男 19

看上面是不是觉得很奇怪,传统对象赋值都是左边四属性,右边是值。但是在解构写法中右边是属性,左边是值,所以新的变量名在右边。

如果使用let、var、const对对象进行解构时,被解构对象的值不能不存在。

不使用var、let、const赋值时,需要将解构语句使用()进行包裹

({type,name} = node);//{}在js中作为代码块,单独使用加等号会报错会报错

嵌套对象解构

在对象嵌套对象中解构,我们会在第一层解构中继续使用花括号来深入下一层进行查找;我们先来看一个栗子:

let node = { type: "Identifier", name: "foo", loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 } }}

上面是一个嵌套对象node,我们先解构第一层

let { loc, type, name } = node // {} Identifier foo

可以看到我们特意打乱了{}中属性的顺序,结果仍然正确输出,所以可以猜到具体的对应方式应该是根据名字来对应的,和顺序无关。

继续解构第二层

let { loc: { start }} = node;console.log(start.line); // 1console.log(start.column); // 4

此处我们也可以将start赋值给一个新的自定义的局部变量,假设我们赋值给newStart

let { loc: { start: newStart }} = nodeconsole.log(newStart.line) // 1console.log(newStart.column) // 4到此,相信大家对"es6解构的概念是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report