Deep replication in Javascript and javascript Replication

Source: Internet
Author: User

Deep replication in Javascript and javascript Replication

In javascript, the value assignment between all object variables is carried to the address. You may want to know which object objects are assigned. It may be better to give an example:

Copy codeThe Code is as follows:
Typeof (true) // "boolean"
Typeof (1) // "number"
Typeof ("1") // "string"
Typeof ({}) // "object"
Typeof ([]) // "object"
Typeof (null) // "object"
Typeof (function () {}) // "function"

So in fact, the main object to be processed during deep replication is the object. For non-object objects, you only need to assign values directly. My idea of implementing deep js replication is:

Traverse all attributes of this object,
If this attribute is "object", special processing is required,
If this object is special and is an array, create a new array and copy the elements in the array in depth.
If this object is a non-array object, call the deep COPY method recursively.
If it is not an "object", you can directly copy it normally.

The following is my implementation:

Copy codeThe Code is as follows:
Object. prototype. DeepCopy = function (){
Var obj, I;
Obj = {};

For (attr in this ){
If (this. hasOwnProperty (attr )){
If (typeof (this [attr]) = "object "){
If (this [attr] === null ){
Obj [attr] = null;
}
Else if (Object. prototype. toString. call (this [attr]) ===' [object Array] ') {
Obj [attr] = [];
For (I = 0; I <this [attr]. length; I ++ ){
Obj [attr]. push (this [attr] [I]. DeepCopy ());
}
} Else {
Obj [attr] = this [attr]. DeepCopy ();
}
} Else {
Obj [attr] = this [attr];
}
}
}
Return obj;
};

If the browser supports ECMAScript 5, you can use

Copy codeThe Code is as follows:
Object. defineProperty (obj, attr, Object. getOwnPropertyDescriptor (this, attr ));

To replace

Copy codeThe Code is as follows:
Obj [attr] = this [attr];

The benefit of implementing this method directly on Object. prototype is that all objects inherit this method. The disadvantage is that some libraries also rewrite Object objects, so sometimes conflicts occur. This is worth noting. The usage is as follows:

Copy codeThe Code is as follows:
Object. prototype. DeepCopy = function (){...}
Var a = {x: 1 };
Var B =;
Var c = a. DeepCopy ();
A. x = 2;
B. x = 3;
Console. log (a. x); // 3
Console. log (B. x); // 3
Console. log (c. x); // 1

The above is an explanation of deep replication. However, since we talk about deep replication today, we want to make a brief summary of the similarities and differences between them.

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.

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.