Detailed description of JavaScript Reference Assignment

Source: Internet
Author: User
JavaScript does not have pointers, and references in JavaScript work differently from most popular programming languages we usually see. In JavaScript, it is impossible to reference one variable to another. In addition, only compound values (such as objects or arrays) can be assigned values through reference. JavaScript does not have pointers, and references in JavaScript work differently from most popular programming languages we usually see. In JavaScript, it is impossible to reference one variable to another. In addition, only compound values (such as objects or arrays) can be assigned values through reference.


The following attributes are used in the entire article:

1. Scalar-a single value or data unit (such as integer, Boolean, string)

2. Composite -- composed of multiple values (such as arrays, objects, and sets)

3. Original-direct value, rather than reference to something containing value.

The scalar type of JavaScript is a primitive. Unlike some other languages (such as Ruby), it has a scalar reference type. Note: In JavaScript, the original scalar value is unchangeable, while the compound value is variable.


Summary:

1. the type of the value assigned to the variable determines whether the value is stored as a value or a reference.

2. When a variable is assigned a value, the original scalar value (Number, String, Boolean, undefined, null, Symbol) is assigned a value, and the composite value is assigned a value through reference.

3. References in JavaScript only point to included values, not to other variables or references.

4. In JavaScript, the original scalar value is unchangeable, and the compound value is variable.


Quick example of value assignment

In the following code snippet, we assign a scalar Original Value (a number) to a variable. Therefore, we assign values here. First, the variable batman is initialized. When the variable superman is assigned a value stored in the batman, a copy of the value is actually created and stored in the variable superman. When the variable superman is modified, the variable batman will not be affected because they point to different values.

Var batman = 7; var superman = batman; // assign a value to superman ++; console. log (batman); // 7console. log (superman); // 8

Quick example by referencing a value assignment

In the following code snippet, we assign a composite value (array) to a variable, so here we assign a value through reference. The variables flash and quicksilver are referenced with the same value (also known as shared value. When the shared value is modified, the reference points to the updated value.

Var flash = [8, 8]; var quicksilver = flash; // assign values to quicksilver by reference. push (0); console. log (flash); // [8, 8, 8, 0] console. log (quicksilver); // [8, 8, 8, 0]

How to create a new reference

When the compound value in the variable is re-assigned, a new reference is created. In JavaScript, unlike most popular programming languages, references point to values stored in variables, not to other variables or references.

Var firestorm = [3, 6, 3]; var atom = firestorm; // assign a value to the console through reference. log (firestorm); // [3, 6, 3] console. log (atom); // [3, 6, 3] atom = [9, 0, 9]; // assign values (create a new reference) to the console. log (firestorm); // [3, 6, 3] console. log (atom); // [9, 0, 9]

How does a reference work when it is passed as a function parameter?

In the following code snippet, the variable magneto is a composite value (an array), so it is assigned as a reference to the variable x (function parameter ).

The Array. prototype. push method called in IIFE changes the value of the variable through JavaScript reference. However, the re-assignment of variable x creates a new reference, and further modification to variable x does not affect the reference of variable magneto.

Var magneto = [8, 4, 8]; (function (x) {// IIFE x. push (99); console. log (x); // [,] x = [, 1]; // re-assign the variable (create a new reference) x. push (88); console. log (x); // [,]}) (magneto); console. log (magneto); // [8, 4, 8, 99]


How to change the original value in a composite variable passed as a function parameter through JavaScript reference

The solution here is to modify the existing compound value that the reference points. In the following code snippet, the variable wolverine is a composite value (an array) and called in IIFE. The variable x (function parameter) is assigned a reference.

You can create an empty Array by setting the value of Array. prototype. length to 0. Therefore, the variable wolverine is changed to a new value in variable x through JavaScript reference.

Var wolverine = [8, 7, 8]; (function (x) {// IIFE x. length = 0; // create an empty array object x. push (,); console. log (x); // [,]}) (wolverine); console. log (wolverine); // [1, 4, 7, 2]


How to store compound values by value assignment

The solution here is to make a manual copy of the compound value, and then allocate the copied value to the variable. Therefore, the reference of the assigned value does not point to the original value.

We recommend that you call the Array. prototype. slice Method to create a (shallow) composite value copy (Array object) without passing any parameters.

Var cisco = [7, 4, 7]; var zoom = cisco. slice (); // create a shortest copy cisco. push (77,33); console. log (zoom); // [7, 4, 7] console. log (cisco); // [7, 4, 7, 77, 33]

How to store a scalar initial value by assigning values by reference

The solution here is to include the original Scalar Value in the composite value (that is, the object or array) as its attribute value. Therefore, it can be assigned a value through reference. In the following code snippet, the original Scalar Value in the variable speed is set to the attribute of the flash Object. Therefore, when calling IIFE, it assigns a value to x (function parameter) by referencing ).

var flash = { speed: 88 };(function (x) {             //IIFE    x.speed = 55;})(flash);console.log(flash.speed);   //55

The above is the detailed description of the JavaScript Reference Assignment. For more information, see other related articles in the first PHP community!

Related Article

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.