JavaScript values and references

Source: Internet
Author: User
Tags scalar

A JavaScript reference points to a Value.

Simple values (that is, scalar primitive type values, basic type values, JS 6 classes, null, undefined, boolean, number, string, and Symbol) are always assigned/passed in the way value is Copied.

For example, at this point, the variable a holds a copy of the value, and variable B holds another copy of him, no matter what kind of action B takes, and no longer affects the value of A.

Composite values-objects (including Arrays and encapsulated objects) and functions are always assigned values/passes by reference to Replication.

As an example of an array, A and B point to two different references to the same composite value [three-to-one], pointing only to, not holding, so if one of a and B changes, they change the original array, and then they point to the new value after the change [1,2,3,4].

A different way of doing B:

At this point, the assignment to b, assigning another array to b,b is no longer a point of reference a, and there is no relation to the point value of a, so the operation of B no longer affects the value of a Point.

Function parameters are often confusing:

Only such operations, in general, can also be understood.

In this case, there will be confusion, using the assignment of the composite value as described earlier, without any effect on the value of the referenced target value [4,5,6], which does not have any effect on the value of the array referenced by a, so a remains [1,2,3,4].

At this point in the function body, there is no assignment class operation, The X operation has always been the reference value of the array, so after the push of the new value, it is the value of the reference is modified, a point is also the changed new value [4,5,6,7].

The operation of a JSON object is similar in principle to an array.

Simple scalar primitive type values (strings and numbers, etc.) are assigned values/passes by value copying, while composite values (objects, Etc.) are assigned values/passes by reference Assignments.

JavaScript values and references

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.