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

How es6 merges objects

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how es6 merges objects. I hope you will get something after reading this article. Let's discuss it together.

Es6 merge object methods are: 1, the use of "Object.assign ()" method, syntax "Object.assign (obj1,obj2,obj3)"; 2, the use of the extension operator "...", syntax "{... obj1,...obj2,...obj3}".

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

Method 1: the Object.assign () method of Es6

The Object.assign () method is used for merging objects, copying all enumerable properties of the source object (source) to the target object (target).

For example:

Let obj1 = {a: 1} let obj2 = {b: 2} let obj3 = {b: 3, let obj 4} let obj = Object.assign (obj1, obj2, obj3) console.log (obj, obj1, obj2, obj3)

Output:

{a: 1, b: 3, c: 4} {a: 1, b: 3, c: 4} {b: 2} {b: 3, c: 4}

Be careful

1. The first parameter of the Object.assign () method is the target object, and the following parameters are all source objects.

2. Here the attribute b in obj2 is overridden by the attribute b in the following obj3.

Method 2: use the extension operator

Let obj1 = {a: 1} let obj2 = {b: 2} let obj3 = {b: 3, obj1,...obj2,...obj3 4} let objTwo = {... obj1,...obj2,...obj3} console.log (objTwo, obj1, obj2, obj3)

Execution result:

{a: 1, b: 3, c: 4} {a: 1, b: 3, c: 4} {b: 2} {b: 3, c: 4}

{... obj1,...obj2,...obj3}... The form of the link is simpler and rougher!

After reading this article, I believe you have a certain understanding of "how es6 merges objects". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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