A line of code to realize the deep cloning of pure data JSON object _javascript technique

Source: Internet
Author: User
Copy Code code as follows:

var dataobjcloned=json.parse (json.stringify (dataobj))

It was seen last night from the little fat microblog on the big city, and it was very interesting, mark.
Today, I sorted out the data to analyze why a sentence can realize the deep cloning of pure data JSON objects.
1.json.stringify function
Copy Code code as follows:

Converts a JavaScript value to a JavaScript object representation (Json) string.
Json.stringify (value [, Replacer] [, space])
Parameters
Value
Necessary. The JavaScript value to convert (usually an object or array).
Replacer
Optional. The function or array that transforms the result.
If Replacer is a function, then json.stringify calls the function and passes in the key and value of each member. Use the return value instead of the original value. If this function returns undefined, the member is excluded. The key of the root object is an empty string: "".
If Replacer is an array, only members with key values in the array are converted. The conversion order of the members is the same as the order of the keys in the array. Replacer arrays are ignored when the value parameter is also an array.
MySpace
Optional. Adds indentation, padding, and line breaks to the return value JSON text to make it easier to read.
If space is omitted, the return value text is generated without any additional whitespace.
If space is a number, the returned value text indents the specified number of spaces at each level. If space is greater than 10, the text indents 10 spaces.
If space is a non-empty string (for example, "\ T"), the return value text indents the number of characters in the string at each level.
If space is a string that is longer than 10 characters, the first 10 characters are used.
return value
A string containing the JSON text.

As you can see from the above introduction, this function converts an object or array into a JSON string.
2.json.parse function
Copy Code code as follows:

Converts a JavaScript object representation (Json) string to an object.
Json.parse (text [, Reviver])
Parameters
Text
Necessary. A valid JSON string.
Reviver
Optional. A function that converts the result. This function will be called for each member of the object. If a member contains nested objects, the nested object is converted before the parent object. For each member, the following occurs:
• If Reviver returns a valid value, the member value is replaced with the converted value.
• If Reviver returns the same value it received, the member value is not modified.
• Delete a member if Reviver returns null or undefined.
return value
An object or array.

As you can see from the above introduction, this function converts a JSON string into an object or an array.
3. Example
Cloning of arrays:
Copy Code code as follows:

var obj = [1,2,[3,4,5]];
var objcloned = json.parse (json.stringify (obj));
Console.log (obj);
Console.log (json.stringify (obj));
Console.log (objcloned);
Objcloned[0] = 6;
Console.log (obj);
Console.log (objcloned);

Experimental Results

From the results above, we found that an array was actually cloned in depth.
Cloning of Objects
Copy Code code as follows:

var obj = {name: ' Rey ', info:{location: ' Beijing ', Age: ' 28 '}};
var objcloned = json.parse (json.stringify (obj));
Console.log (obj);
Console.log (json.stringify (obj));
Console.log (objcloned);
Console.log (Json.stringify (objcloned));
Objcloned.name = ' Luopan ';
Console.log (obj);
Console.log (json.stringify (obj));
Console.log (objcloned);
Console.log (Json.stringify (objcloned));

Experimental Results

From the above experiment we found that such a method can also clone objects.
4. But all of the above experiments are for pure data, that is, this method is only valid in an array of pure data or in an object clone.
experiment of non-pure data
Copy Code code as follows:

var obj = {name: ' Rey ', info:{location: ' Beijing ', Age: '},hello:function ' () {console.log (' Hello world! ');};
var objcloned = json.parse (json.stringify (obj));
Console.log (obj);
Console.log (json.stringify (obj));
Console.log (objcloned);
Console.log (Json.stringify (objcloned));
Objcloned.name = ' Luopan ';
Console.log (obj);
Console.log (json.stringify (obj));
Console.log (objcloned);
Console.log (Json.stringify (objcloned));

Experimental Results

From the experimental results above, it can be seen that the function of the impure data cannot participate in the conversion, and it is "despised".
Therefore, this sentence depth cloning method only for pure data, this is the development of the need to pay attention to the place.

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.