Home > Web Front-end > JS Tutorial > body text

A brief discussion on Javascript medium and deep copy_Basic knowledge

WBOY
Release: 2016-05-16 16:29:57
Original
1382 people have browsed it

In JavaScript, all assignments between object variables pass addresses. Some students may ask which objects are objects. It might be better to give an example:

Copy code The 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 we need to process in deep copy is the object object. Non-object objects only need to be assigned directly and normally. My idea of ​​​​implementing js deep copy is:

Traverse all the properties of the object,
If the attribute is "object", special handling is required,
If this object is special and is an array, then create a new array and deeply copy the elements in the array
If this object is a non-array object, just call the deep copy method recursively on it.
If it is not "object", just copy it normally.

The following is my implementation:

Copy code The 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               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, in order to deep copy all properties of the object properties, you can use

Copy code The code is as follows:

Object.defineProperty(obj, attr, Object.getOwnPropertyDescriptor(this, attr));

to replace

Copy code The code is as follows:

obj[attr] = this[attr];

The advantage of implementing this method directly on Object.prototype is that all objects will inherit this method. The disadvantage is that some libraries also rewrite Object objects, so conflicts sometimes occur. This is something to note. The specific usage methods are as follows:

Copy code The code is as follows:

Object.prototype.DeepCopy = function () { ... }
var a = { x:1 };
var b = a;
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 the explanation about deep copy, but since we talked about deep copy today, we also want to correspond to shallow copy. Let’s briefly summarize the similarities and differences between them.

Shallow copy (shadow clone): Only the basic type and object type of the object are copied, which still belongs to the original reference.
Deep copy (deep clone): not only copies the basic class of the object, but also copies the objects in the original object. That is to say, completely new objects are generated.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!