Cloning in JavaScript

Source: Internet
Author: User

One: node cloning

var p = document.getelementsbytagname ("P") [0];

var CP = P.clonenode ();//Clone P-node

var CP = P.clonenode (TRUE);//clone P node, deep clone, clone node and sub-content under node.

Two-variable cloning

① Basic types of clones

The basic type of replication, in fact, is to create a new value on the variable object, and then copy the value to be copied to the location of the new variable allocation, when the value of the variable is changed, the other value is not affected, passed by value.

(The parameters of a function can only be passed by value), which is deep cloning, and the copied object is completely independent of the new object.

② a clone of a reference type

Also copies a copy to the space allocated for the new variable, but the copied variable is a pointer to the same object stored in the heap. If you change the value of one of the variables, the other changes and is passed by the pointer.

That is, shallow cloning.

Three: Array cloning

①slice ()

1 var aarr = [0,1,2,3];

2 var m = aarr.slice (0);

3 Aarr = [3,2,1,0];

After cloning, when changing the Aarr, M will not change, realize the deep cloning of the array.

②concat ()

Implements the merging of arrays, merging the current array and an empty array.

③ the simplest way to use a loop to push () the elements in the original array into a new, empty array.

Cloning in JavaScript

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.