<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="jquery.min.js"></script> <script> $(function() { //1, normal copy, shallow copy by default var targetObj = {}; var obj = { id: 1, name: "andy" }; $.extend(targetObj, obj); //Copy syntax, copy the attributes and attribute values in obj to targetObj an empty object console.log(targetObj); //The output result is the content of the obj object //2. If there are attributes and attribute values in the copied object var targett = { id: 0, gshg: 55 }; var objrr = { id: 1, name: "andy" }; $.extend(targett, objrr); console.log(targett); // If the properties in the copy object targett are the same as the properties in the copied object objrr, the property values in objrr will overwrite the property values in targett // If the properties in the copy object targett are different from the properties in the copied object objrr, they will not be overwritten, and the properties in the copied object objrr will only come later // 3. Shallow copy when there are objects in the copy object var taytuj = { id: 0, msg: { sex: 'male' } }; var return = { id: 1, name: "andy", msg: { age: 18 } }; $.extend(true, taytuj, rete); console.log(taytuj); // Execution process: //1. Both objects are in the memory stack, and the msg: {age: 18} object in the rete object is placed in the memory heap. In the shallow copy, the msg: {age: 18} in the rete object will overwrite the msg: {sex: 'male'} of the taytu object, and the attribute value will become a hexadecimal pointing to the msg: {age: 18} object in the memory heap. At this time, the msg of the two objects in the stack space have the same hexadecimal address pointing to the msg: {age: 18} in the memory heap, so if the shallow copy is completed, as long as one of the msg attribute values in the two objects changes, the attribute value of the other msg will also change together. //Shallow copy: //1. If the attribute names are the same, the copied object will overwrite the copy object, and the order will always be the last one to come first. //2. If the attribute names are different, they will be merged. //Deep copy: //1. If the attribute names are the same, the attributes and attribute values of the copied object will overwrite the attributes and attribute values of the copy object, and the sorting is always from the later to the higher. //2. If the attribute names are different, they will be merged. //3. If the copied object and the object inside the copied object have the same object name but different attribute names, they will be merged instead of overwritten. }) </script> </head> <body> </body> </html> SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Web data storage: Cookie, UserData, SessionStorage, WebSqlDatabase
MySQL Daemon failed to start error solution A few...
1. Overview This article systematically explains ...
1. Single table query -> update UPDATE table_n...
Table of contents 1. forEach() 2. arr.filter() 3....
I encountered a problem when modifying the defaul...
Docker officially recommends that we use port map...
Overview UNION The connection data set keyword ca...
This article records the installation graphic tut...
Data Types and Operations Data Table 1.1 MySQL ty...
Preface When developing a project, I encountered ...
Let's first understand a wave of concepts, wh...
Table of contents Overview 1. useState 1.1 Three ...
The backend uses the thinkphp3.2.3 framework. If ...
Table of contents 1. redo log (transaction log of...
In many apps and websites, when we log in or regi...