Previously, I saw the blog's deep replication method for Json objects, that is
Copy codeThe Code is as follows: var obj = {
SayName: function (){
Alert (this. name );
},
Name: 'hydrostatic yuan'
}; Var cloneObj = JSON. parse (JSON. stringify (obj); cloneObj. sayName ();
However, in this way, the attribute value cannot be copied as a function attribute. Therefore, the following code is used to improve the method:Copy codeThe Code is as follows: var obj = {
SayName: function (){
Alert (this. name );
},
Name: 'hydrostatic yuan'
};
Function clone (){
Var str, newObj;
Str = JSON. stringify (obj, function (key, value ){
Return (typeof value = 'function '? Value. toString (). replace (/^ function (. *)/g, "jsonFunction $1"): value );
});
NewObj = JSON. parse (str, function (key, value ){
If (/^ jsonFunction (. *)/. test (value )){
Var strFun = '(' + value. replace (/^ jsonFunction (. *)/, "function $1") + ')';
Value = eval (strFun );
}
Return value;
});
Return newObj;
}
Var cloneObj = clone (obj );
CloneObj. sayName ();
Because you haven't fully tested it yet. Please be prepared!