$.extend (true,{},a,b), deep understanding, beware of traps

Source: Internet
Author: User

$.extend
In general, when using depth to expand two objects, the effect we want is that the B object overrides all the attributes that exist in the A object, and none is added to the A object;
Let's take a look at the problems I've encountered:

I hope that the editoption in TMP will be covered by the deep expansion of the editoption in the RET, so the depth extension method is adopted ret = $.extend (true,{},ret,tmp);
But the result didn't work out the effect I wanted:

The length of the array in the object properties is still unchanged.
The example shows that although the array length has not changed, the value of the first 8 bits in the array is changed, that is to say, the variable depth extension overrides the value of the array, rather than overwriting the entire array object.

Note If you use $.extend (A, B) directly, the A object value is changed. Notice the Magic bug that the object reference throws

Because $.extend (A, A, a, b) returns a result after merging a with a, the A object will be changed.

Finally I use $.extend ({},a,b) to solve this requirement: that is, I do not change the reference of a A, B object, and I return the value I want.

Summary: Even simple methods, but also need to understand deeply, to avoid the subconscious cognitive interference in the judgment of things.

$.extend (true,{},a,b), deep understanding, beware of traps

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.