This was originally a question in stackoverflow, after seeing the answer to benefit a lot, so the translation under share with you, the original address: http://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript
I won't talk about the shallow copy. All of them are reference objects. Some examples of the deep copy listed on the Internet also have more or less problems. I have tried it, in some special cases, a strange problem may occur. Here is a perfect deep replication function (do not copy it directly for use, and there is interactive content in it ):
Function clone (item) {If (! ITEM) {return item;} // null, undefined values check var types = [number, String, Boolean], result; // normalizing primitives if someone did new string ('aaa'), or new number ('000000'); // some types of things created using the new method may change, here we need to normalize the processing. // For example, new string ('aaa'), or new number ('123') types. foreach (function (type) {If (item instanceof type) {result = type (item) ;}}); If (typeof result = "undefined") {If (OB Ject. prototype. tostring. call (item) = "[object array]") {result = []; item. foreach (function (child, index, array) {result [Index] = clone (child) ;});} else if (typeof item = "object ") {// testign that This is Dom // if it is a DOM object, use the built-in clonenode to process if (item. nodetype & typeof item. clonenode = "function") {var result = item. clonenode (true);} else if (! Item. prototype) {// check that this is a literal // It is an object literal // if it is an object iteration, we can use the for in iteration to assign result = {}; for (var I in item) {result [I] = clone (item [I]) ;}} else {// depending what you wowould like here, // just keep the reference, or create new object // here the constructor is solved. Here, we need to check how you want to copy the object. If you have a deep understanding, remove the false &&, otherwise, maintain the original reference, // but I do not recommend that you use a new constructor for deep replication. The specific reason is as follows: if (false & item. constructor ){ // Wocould not advice to do that, reason? Read below // struct we do not recommend that you go to the new constructor result = new item. constructor () ;}else {result = item ;}} else {result = item ;}} return result ;}var copy = clone ({one: {'one-one': New String ("hello"), 'one-two': ["one", "two", true, "four"]}, two: document. createelement ("Div"), three: [{name: "three-one", number: new number ("100"), OBJ: New Function () {This. name = "Object test" ;}}]})
The above function does not have any problem in copying some things in {}. The situation is comprehensive and comprehensive. However, we have not completed the calculation yet. Let's discuss how to copy a "real" object. For example, you have created an object through the constructor:
var User = function(){};var newuser = new User();
In this case, it is completely okay to use the above functions. All the attributes of all objects are exposed. In other words, all objects are public, public, and accessible. You can use for in to iterate the attributes, no matter what type, remove the "false &" to use it. It is no problem at all. However, there is a risk here-you must know that the attributes of objects or instances cannot all be public. Once a private variable (State state in the original article) exists ), such a copy is meaningless, because it will lose the data. For example, the following code:
function Man() { var age = 1; this.getAge = function () { return age; } this.grow = function () { age += 1; }}var man = new Man();
The age in it will be unable to be copied.
In addition, there is another problem. If you think there is no such private variable, there is a problem with the Code. However, some object constructors have parameters, for example, I want to create an object like this:
new User({ bike : someBikeInstance});
In this case, you are estimated to be tragic. somebikeinstance may be created in other contexts. We cannot get this, at least for this function ..
What should we do? In fact, there is no good solution. Either we use for in to iterate, discard those private variables, or use references, which will cause more or less problems, another solution is to create a copy function attribute in all the objects to be copied, just like the copy node function clonenode In the Dom.
Sorry for the first translation error! Pai_^