Copy, deep copy and shallow copy.
In the framework we've learned, jquery's $.extend () can differentiate the copy type based on the first incoming parameter;
jquery.extend (Target [, Object1] [, objectn]);
- Deep Type: Boolean if true, merging becomes recursive (also known as a dark copy).
- target Type: Object extension. This will receive the new attribute.
- object1 Type: An object that contains additional attributes merged into the first parameter.
- objectn Type: object contains additional attributes merged into the first parameter
function Shallowcopy (srcobj, destobj) {var destobj = Destobj | | {};for (var key in srcobj) {if (!destobj.hasownproperty (key)) {Destobj[key] = Srcobj[key];}} return destobj;}
Shallow copy is just a simple cyclic assignment, so it is very efficient and fast.
function Deepcopy (srcobj, destobj) {var destobj = Destobj | | {};for (var key in srcobj) {if (!destobj.hasownproperty (key)) {if (typeof srcobj[key] = = ' object ') {Destobj[key] = (srcobj[ Key].constructor = = = Array)? []: {};d eepcopy (Srcobj[key], Destobj[key]);} Else{destobj[key] = Srcobj[key];}} return destobj;}
The core of a deep copy is recursion.
JS design mode-Copy mode