Introduction to Javascript Article 2 js Type page 1/2

Source: Internet
Author: User

1. Conversion between objects and basic types:
No matter when the object is not null, it is true in a Boolean environment.
For example;
New Boolean (false );
New Number (0 );
New String ("");
New Array ();
Although the internal value is false, the object value is true;
Object? ValueOf ()? ToString ()
In the Date class, toString () conversion is performed first.

2. operate a data value in js:
Methods for operating data in any language;
Js is no exception. js has three important methods to operate a data value.
1) copy it. For example, assign it to a new variable.
2) pass it as a parameter to a function or method.
3) Compare the value with other values.

Js operates the values of these data in two ways: Pass the value and transfer the address.
You can see from the name that the data is operated by passing the value. During the assignment process, the actual values are copied and stored in a new variable. The copied value and the original value are two completely independent values. Therefore, if you change the copy value, the original value will not be affected. When the data size is compared, the data is usually compared in bytes.
From the perspective of the name, the address is transferred to operate the data. During the value assignment process, the actual value addresses (which can be referenced) are copied. They are not completely independent. Therefore, if you change the value through reference, the original value will also change. When comparing the size, it usually depends on whether they reference the same address for comparison.
Simple address transfer example:
Var a = new Date ();
Alert (a. getDate ());
Var B =;
B. setDate (21 );
Alert (a. getDate () // Output 21

3. In general:
The basic data type is operated by passing values. (If you forget the basic data types, you can go back to them .)
The object data type is operated by transferring the address. (Such as arrays and functions)
Example:
<Script>
// Pass the value
A = 1;
B =;
B = 2;
Alert (a); // output 1

// Transfer the address
X = [1, 2];
Y = x; // assigned to y is only a reference of x, not x itself. The array has been assigned a value in the statement. After executing this code, there is still only one array object, but we have two references to it.
Y [0] = 2;
Alert (x [0] + "|" + x [1]); // output 2 | 2
</Script>
Note the following strings:
In js, strings are copied and transferred through address transfer, and they are compared by passing values.
Objects and arrays are passed by passing values, but the passed value is actually a reference, not the object itself.
Summary:
Type replication transfer comparison
Pass numeric values
Boolean value transfer value
Immutable and immutable string value passing
Object address transfer address
Immutable: In JS, there is no way to change the content of the string value.
For a string, it is of little significance to pass the value or the address.

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.