JS Array Reference found problems

Source: Internet
Author: User

The most recent project is to make a change to the returned data [saved in the JSON array], but the original data should be kept once for backup. First of all, the original data is not moving, with a temporary variable to modify, the approximate model is this:

// Original: a=[1,2,3,4,5,.........]; // Temporary: var b = A; // Operation:  = 1;

I thought it was a very simple question. But the test found that it did not get the desired results. It took a long time to find the problem: a data is actually followed by the operation of the B has changed, how can not make sense. Asked colleagues, it seems that he has not met, but also do not know how to go. Had to ask for help network query. Found a problem similar to mine.

There is an example of this:

<script>vara=[1,2,3,4]; //Example 1     varb=A;   alert (a); //1234alert (b);//1234   //Example 2    varC=A; c[3]=5;   alert (c); //1235alert (a);//1235   //Example 3    varD=a[1]; D=10;   alert (a); //1234</script>//Conclusion: Arrays are reference types, array pairs are value types

One of them gave an explanation:

There is no pointer in JS, only the difference between the value and the address (reference reference)

var a = [1,2,3,4]//a is not only an array, but also an object, in fact A is a reference to [1,2,3,4]
var b=a
var c=a
The above two assignment statements establish a reference to B and C for a, [1,2,3,4], whether changing a or B or C are operations on [1,2,3,4], which is the address (in-heap operation)

var d=a[1]//is the value of a[1] "1" passed to D, the change of D will not affect a[1], that is, the so-called value (operation in the stack)

That's a little bit clear. Or with the original conclusion:

JS Array is a reference type, it only allows to get or change the value of the array by index reference type things are not through (it is assigned to the external variable) is changed (it is assigned to the external variable) this value changes the original array will not have any changes

The original point here

JS Array Reference found problems

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.