Chapter 2 data types and values of the rhino book (Part 2): Data Transfer and data transfer

Source: Internet
Author: User

In JavaScript, as in all other programming languages, you can operate a data value in three ways:

You can copy it. For example, you can assign it to a new variable.

You can pass it as a parameter to a function or method.

You can compare it with another value to see if the two values are equal.

To understand any programming language, you must understand how these three operations are implemented in this language.

There are two completely different ways to operate data values. These two technologies are called value transfer and address transfer respectively.

Pass Value: When a data is operated by a value, it is related to the value of the data. During the value assignment process, the actual value is copied, which is stored in a variable, object attribute, or array element. The copied value and the original value are two completely independent values respectively stored. When a copy of data is passed to a function through a value, a copy of the data is passed to this function. If the function modifies this value, modification only affects the copy of the data owned by the function, and does not affect the original data. It is recommended that when one data is compared with another data through a value, the two completely different data fragments must indeed represent the same value (which usually means that they are equal after byte-by-byte comparison ).

Address Transfer: Using this method, there is only one copy of the value, and the operation is to reference the value (address ). If you operate a value in the form of an address, the variables do not directly store the value. They only store the address of the value. All the copied, transmitted, and compared values are the address. Therefore, in the assignment operation, the address of the value is assigned, not a copy of the value, nor the value itself. After a value is assigned, the new variable points to the same value as the original variable. Both addresses are valid and can be used to operate this value. If the value changes through an address, this change will also be expressed through the original address. When a value is passed to the function through address transfer, the situation is similar.

These are two different methods of operation values. They have very important meanings and should be understood.

1. Basic type and reference type:

The basic rule of JavaScript is that the basic data type is operated by passing values, and the reference type is operated by passing addresses.

2. Copy and pass strings:

Javascript strings cannot be well suited to the basic type and the binary method of the reference type. So how does a javascript string pass and copy strings? If we write some JavaScript code to test the string operation, we will encounter an important obstacle: there is no way to modify the content of a string. Because the javascript string is immutable, that is to say, there is no way to change the characters in the string. Since the string is immutable, the initial problem becomes meaningless, because there is no way to distinguish whether the string is passed by passing the value or by passing the address.

3. compare strings:

Regardless of the facts, we speculate from the perspective of efficiency that JavaScript strings are copied and transmitted through address transfer, and they are compared by passing values.

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.