jQuery object copy

jQuery object copy

If you want to copy (merge) an object to another object, you can use the $.extend() method at this time

grammar:

$.extend([deep],target,object1,[objectN])

  1. deep: If set to true for deep copy, the default is false shallow copy
  2. target: Object type target object, member attributes of other objects will be attached to this object
  3. object1: Optional, the first object of the Object type to be merged.
  4. objectN: Optional, the Nth merged object of the Object type.
  5. Shallow copy is to copy the address in the complex data type of the copied object to the target object. Modifying the target object will affect the copied object
  6. Deep copy, add true in front, complete clone (the copied object, not the address), modifying the target object will not affect the copied object
var targetObj = {
    
    };
var obj = {
    
    
  id:1,
  name:"andy"
}
//$.extend(target, obj);
$.extend(targetObj, obj);

note:

  1. If only one parameter is specified for $.extend(), it means that the target parameter is omitted. At this point, the target is the jQuery object itself. through ways. We can add new functions to the global object jQuery
  2. If multiple objects have the same properties, the latter will override the former's property value

Guess you like

Origin blog.csdn.net/qq_46178261/article/details/106219308