"Deep Copy Pit" 1, object assign copy of False depth, 2, array slice copy pits, 3, and array map copy pits

Source: Internet
Author: User
Tags shallow copy

1, the object copy Pit Object.assign ()

Object.assign ()  // shallow copy basic useless with direct = same as object.assign (true//  Think is a deep copy, in fact, is only a level of attribute replication, more than a shallow copy of a layer of deep copy. The second layer is a shallow copy, which changes the original value

2, the array copy of the pit, even if the slice copy, if the internal is an object, or will address point, resulting in the modification of the new array, the old array also changed

var arr = [{a:11},{a:11}];  // arr [{a:11},{a:11}] var arr1 = arr.slice (0);   // arr1 [{a:11},{a:11}]arr1[0].a =  +////arr1 [{a:22},{a:11}]  arr [{a:22},{a:11}]

As it turns out, slice replication is just a sneak copy, and cannot be deeply copied object content

3, also is the array copy pit, if the array content element is the object, the map will also change the original value

var // arr  [{a:11},{a:11}]  Arr.map (v=>{v.b=22;  return// are you too naïve to think this arr has no half-dime relationship, ARR has changed //arr  [{A : 11,b:22},{a:11,b:22}]  has come up with a B-property .... 

"Deep Copy Pit" 1, object assign copy of False depth, 2, array slice copy pits, 3, and array map copy pits

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.