ES6 extension Operator (three-point operator) "..." Usage and object copy

Source: Internet
Author: User

The ES6 copy Array object has the following methods:

method One: object.assign () // object Shallow Copy, obj1 Copy to Obj2, this method to set Obj2 {}, not const OBJ2 = ""; Const OBJ1 = {a:1= {};object.assign (Obj2, Obj1) method two: ES6 extension operator (...) // object Shallow Copy, obj1 copy to obj2const OBJ1 = {a:1= {... obj1}; method Three: Deep copy // object deep Copy, obj1 copy to obj2< /c10>const obj1 = {a:1= Json.parse (json.stringify (obj1));

So-called deep copy and shallow copy:

Const OBJ1 = {a:1=  = 2console.log (obj1)   //{a:2}

A direct assignment like this is in fact a obj2 reference obj1, which points to the same storage address, regardless of which of the two will be affected.

For simple array objects (which do not contain reference data types), you can use a shallow copy method to eliminate this association effect.

For complex array objects (with reference data types, such as: {A:1,b:[1,2,3],c:{d:3}}} multiple nested types), a deep copy method is required.

In a real project, this is usually the case:

 This true}). Then (res) =      {if  (res.data) {
Console.log (res.data) This . evaluationinfo = Res.data else { ... }})

Console.log print out will find that the RES.DATA data structure has changed some of the object's get and set functions, such a direct assignment is a reference, although not the data produced a bug, but if other places also need to use to Res.data, like this:

 This true}). Then (res) = {      if  (res.data) {         console.log (res.data)         this. Evaluationinfo = Res.data         this. selectevaluationinfo = res.data      Else  {         ...      }})

In this case, you need to use the object copy method to differentiate, you can use the three-point operator is simpler:

 This true}). Then (res) = {      if  (res.data) {         console.log (res.data)         this. Evaluationinfo = {... res.data}this. selectevaluationinfo =          {... res.data}       Else  {         ...      }})

Copying an object is just one use of the three-point operator, and it has several other uses, often used in array serialization:

function f (x, Y, z) {    //  ... }var args = [0, 1, 2];f (.... args)

ES6 extension Operator (three-point operator) "..." Usage and object copy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.