The merging and cloning of JS----objects

Source: Internet
Author: User
Tags hasownproperty

I. Differences in merger and cloning

1. Clones are special merges (with empty objects as target objects, non-empty objects are merged as source objects), and cloning requires that the target object be the same as the constructor of the source object.

2. The cloned source object has only one, and the merged source object can be multiple.

Two. Method of merging

1.object.assign ():

Example: Var obj1 ={

M:1,

N:2,

J: {

R: {

H:2

},

P:4},

P:1

}

var obj2 ={M:2, n:undefined, J: {H:2, O:3}}

var obj3 = object.assign (OBJ1,OBJ2);

Result: Obj1 = {m:2,n:undefined, J: {H:2, o:3}, p:1};

Obj2 ={M:2, n:undefined, J: {H:2, o:3}};

Obj3 ={M:2, n:undefined, J: {H:2, o:3}, p:1};

Note: 1). The target object itself will also become obj1===obj3

2). This method is a shallow merge

3). Undefined participate in the merger

4). Prototype not attribute join merge

2. $.extend:

Situation One:

Example: Var obj1 ={M:1, N:2, J: {r: {h:2}, P:4}, P:1}

var obj2 ={M:2, n:undefined, J: {H:2, O:3}}

var obj3 = $.extend (OBJ1,OBJ2);

Results: obj1 = {m:2, N:2, J: {H:2, o:3}, p:1};

Obj2 ={M:2, n:undefined, J: {H:2, o:3}};

Obj3 ={M:2, N:2, J: {H:2, o:3}, p:1};

Note: 1). The target object itself will also become obj1===obj3

2). This method is a shallow merge

3). Undefined does not participate in the merger

4). Prototype not attribute join merge

Situation Two:

Example: Var obj1 ={M:1, N:2, J: {r: {h:2}, P:4}, P:1}

var obj2 ={M:2, n:undefined, J: {H:2, O:3}}

var obj4 = $.extend ({},obj1,obj2);

Result: Obj1 = {m:1, N:2, J: {r: {h:2}, P:4}, p:1};

Obj2 ={M:2, n:undefined, J: {H:2, o:3}};

Obj3 ={M:2, N:2, J: {H:2, o:3}, p:1};

Note: 1). This method is a shallow merge

2). Undefined does not participate in the merger

3). Prototype not attribute join merge

Situation Three:

Example: Var obj1 ={M:1,n:2,j:{r:{h:2},p:4},p:1}

var obj2 ={m:2,n:undefined,j:{h:2,o:3}}

var obj3 = $.extend (TRUE,OBJ1,OBJ2);

Result: Obj1 = {m:2,n:2,j:{h:2,o:3,r:{h:2},p:4},p:1};

Obj2 ={m:2,n:undefined,j:{h:2,o:3}};

Obj3 ={m:2,n:2,j:{h:2,o:3,r:{h:2},p:4},p:1};

Note: 1). The target object itself will also become obj1===obj3

2). This method is a deep merge

3). Undefined does not participate in the merger

4). Prototype not attribute join merge

3. Traversal Assignment method

Idea: Assign a value to Obj1 for a property that exists in Obj2 but obj1 not exist.

Steps: 1). Iterates through the properties in the Obj2.

2). Determine that obj1 does not exist for this property

3). Assign a secondary value to Obj1

var extentobj = function (obj1,obj2) {

For (let key in Obj2) {

if (Obj2.hasownproperty (key) && (!obj1.hasownproperty (key)) {

Obj1[key] = Obj2[key]

}

}

}

Three. Methods of cloning

1. Json.parse (Json.stringify ()):

1). Turn the object into a string and then into a JSON object to prevent the object's pointer from pointing to the problem, for a deep copy

2). The properties of the undefined and function types are ignored, and the properties of the Date type are converted to strings

2. $.extend:

True is a deep copy, not a shallow copy

Note: The difference between a deep copy and a shallow copy

A shallow copy takes the reference address as it is, regardless of whether the source object or target object is modified, and the property of the same name is affected by the other object after modifying the reference property.

A deep copy recursively creates a value on the target object, and the target object and the source object are completely independent

The merging and cloning of JS----objects

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.