Implementation Code of JavaScript light copy and deep copy

Source: Internet
Author: User

What is "clone "?
In the actual programming process, we often encounter this situation: there is an object A, which contains some valid values at a time point, in this case, you may need a new object B that is exactly the same as a, and any subsequent changes to B will not affect the value in A. That is to say, A and B are two independent objects, however, the initial value of B is determined by object. In Java/javasript, a simple value assignment statement cannot meet this requirement. Although there are many ways to meet this requirement, the clone () method is the simplest and most efficient method. Of course, this method is not used in JavaScript.
Therefore, I specifically wrote two cloning methods: one for shortest replication and the other for deep replication.
Explanation:
Light copy (Shadow Clone): Only copies the basic type of the object. The object type still belongs to the original reference.
Deep replication (deep cloning): Copies the basic class of the object and the object in the original object, that is, completely generated by the new object. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> <meta name =" generator "content =" Wawa editor 1.0 "> <meta name =" author "content =" Eight gods "> <meta name =" keywords "content =" JavaScript, java, XML, XSLT, ASP, VBScript, Asp. net, C #, C ++, Database "> <meta name =" Description "content =" unqualified programmers, ubiquitous ">
Related Article

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.