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 function of es6 deconstructing assignment

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

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "what is the role of es6 deconstruction assignment". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the role of es6 deconstruction assignment" can help you solve your doubts.

Function: 1, exchange the value of two variables, syntax "[XMagne y] = [yMagnex];"; 2, decompose the string, the character into the variable, syntax "[variable list] = string"; 3, return multiple values from the function, syntax "[variable list] = function"; 4, extract json data, syntax "{variable list} = json variable" and so on.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

Es6 deconstruction assignment

Es6 allows you to extract values from arrays or objects and assign values to variables according to a certain pattern, which is called deconstruction assignment.

Deconstruction assignment is easy to understand in code writing, and its semantics is clear, so it is convenient to obtain data fields in complex objects.

Deconstruction model

In deconstruction, the source of the deconstruction is on the right side of the deconstruction assignment expression, while the target of the deconstruction is on the left side of the deconstruction expression.

Deconstructing the role of assignment

1. Swap the value of the XBI y variable

By using deconstruction assignment, the values of two variables can be exchanged without the help of the third variable.

Let x = 1; let y = 2; console.log ('x v x,'y v v y); / x v v 1 y v v 2 / / structure assignment [x Magi y] = [y department x]; console.log ('x v x,'y v v y); / x v v 2 y v 1

2. Decompose the string and pass the characters into the variable

Var] = "hello"; console.log (a); / / hconsole.log (b); / / econsole.log (c); / / lconsole.log (d); / / lconsole.log (e); / / o

3. Return multiple values from the function

The / / function can only return one value, and if you want to return multiple values, you should return them in an array or object.

Function example () {/ / returns an array of return [1mem2helo 3]} let [arecedence brecedence c] = example (); console.log ('arecedicles). / / an object return {name:'kang+', age:20}} let {name,age} = example2 (); console.log ('name:'+name,'age:'+age); / / name:kang+ age:20

4. Definition of function parameters.

/ / the parameter is an ordered set of values function f ([xrecedence z]) {console.log ('xrecuperjinghex); console.log (' yreguanjuy); console.log ('zjjanjuz);} f ([1JI 2JI 3]) / / the parameter is an unordered set of values function f ({xrector ydagz}) {console.log ('XRV'); console.log ('YRV'); console.log ('ZRV');} f ({ZRV 3, XRO 2, YRO 1})

5. Extract json data (which must be used in project development)

Let jsonData = {number:01, status:'200', data: [{person:'kang+',age:20}]} / / get json data let {number,status,data,data: [{person,age}]} = jsonData; console.log ('number:'+number); / / number:1 console.log (' status:'+status) / / status:200 console.log (data) / / [{… }] console.log ('person:'+person); / / person:kang+ console.log (' age:'+age); / / age:20

6. the default value of the function parameter (this individual thinks it is of little use)

Function Person (name,age,school = 'xiyou') {this.name = name; this.age = age; this.school = school;} var son = new Person (' kang+',20); console.log (son) / / {name: "kang+", age: 20, school: "xiyou"}

7. Traversing the map structure

The Map structure natively supports the Iterator interface, which is very convenient to obtain the key name and key value with the deconstruction assignment of variables. Var map = new Map (); map.set ('name','kang+'); map.set (' age','20'); for (let [key, value] of map) {console.log (key +'is'+ value);} name is kang+ age is 20 get key name var map = new Map (); map.set ('name','kang+'); map.set (' age','20') For (let [key] of map) {console.log (key); / / name age} / / get the key value var map = new Map (); map.set ('name','kang+'); map.set (' age','20'); for (let [, value] of map) {console.log (value) / / kang+ 20} here, the article "what is the role of es6 deconstruction assignment" has been introduced. To master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

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