Easy to learn about JavaScript 11: Basic JavaScript types (including type conversion) and reference types

Source: Internet
Author: User

Easy to learn about JavaScript 11: Basic JavaScript types (including type conversion) and reference types
Type of a value

I mentioned the basic and reference types as early as I introduced JS data types. But before talking about the two types, let's take a look at the variable's

Value Type. In ECMAScript, a variable can have two types of values: the original value and the reference value.

(1) Original Value

Simple data segments stored in the stack, that is, their values are directly stored in the variable access location.

(2) Reference Value

Objects stored in the heap, that is, the value stored in the variable is a pointer pointing to the memory of the stored object.

When assigning values to variables, the ECMAScript interpreter must determine whether the value is of the original type or reference type. To achieve this, the interpreter needs

Determine whether the value is one of the basic types of ECMAScript, that is, Undefined type, Null type, Boolean type, Number type, and

String type. Because the space occupied by these basic types is fixed, they can be stored in a small memory area-stack. This makes storage easier

Returns the value of a variable.

In many languages, strings are considered as reference types rather than basic types, because the length of strings is variable. ECMAScript broke this

Traditional.

If a value is of the reference type, its storage space will be allocated from the heap. Because the size of the reference value changes, you cannot place it on the stack.

Otherwise, the variable search speed is reduced. On the contrary, the value in the stack space of the variable is the address of the object stored in the heap. The address size is fixed.

So storing it in the stack has no negative impact on variable performance. As shown in:

2. Basic Types

ECMAScript has five basic types: Undefined, Null, Boolean, Number, and String. ECMA-

262 define a term type as a set of values. Each basic type defines the range of values it contains and their literal representation.

ECMAScript provides the typeof operator to determine whether a value is within a certain type. You can use this operator to determine whether a value represents

A basic type: if it is a basic type, you can also determine which basic type it represents.

The basic data types and operators typeof are also frequently used in the previous blog. For more information, see my blog: easy to learn

JavaScript 5: JavaScript variables and data types.

Three-type conversion

One of the most important features of all programming languages is the ability to perform type conversion. ECMAScript provides developers with a lot of simple type conversion

Change method. Most types have simple conversion methods, and several global methods can be used for more complex conversions. In either case

In ECMAScript, type conversion is a short step.

(1) convert to a string

ECMAScript's Boolean values, numbers, and String Primitive values are interesting because they are pseudo objects, which means they actually have properties.

And methods.

For example, to obtain the length of a string, you can use the following code:

 

Var sbox = "red"; document. write (sbox. length); // output 3

 

Although "red" is a string of the basic type, it still has the property length, used to store the size of the string. All in all, three main primitive classes

Type Boolean values, numbers, and strings all use the toString () method to convert their values into strings. You may ask, "the string also has

Is the toString () method unnecessary ?" Yes, indeed, but ECMAScript defines that all objects have the toString () method, whether it is a pseudo

Object or a real object. Because the String type is a pseudo object, it must have the toString () method.

1) The toString () method of the Boolean type only outputs "true" or "false", and the result is determined by the value of the variable:

 

Var bage = false; document. write (bage. toString (); // output "false"

 

2) The toString () method of the Number type is special. It has two modes: default mode and base mode. Use the default mode, toString () method

Only use the corresponding string to output the numeric value (whether it is an integer, floating point number, or scientific Notation ),

In the default mode, No matter what notation is used to declare a Number, the toString () method of the Number type returns a decimal table of numbers.

. Therefore, all numbers declared in octal or hexadecimal literal form are output in decimal form. As follows:

 

Var iNum1 = 10; var iNum2 = 10.0; document. write (iNum1.toString (); // output "10" document. write (iNum2.toString (); // output "10"

 

The base mode of the toString () method of the Number type. Different base numbers can be used to output. For example, the binary base is 2 and the octal base is 8,

The base of the hexadecimal format is 16.

The base is just another addition of the base to be converted to. It is a parameter of the toString () method:

 

Var iNum = 10; document. write (iNum. toString (2); // output "1010" document. write (iNum. toString (8); // output "12" document. write (iNum. toString (16); // output ""

 

(2) convert to a number

ECMAScript provides two methods to convert non-numeric values into numbers: parseInt () and parseFloat (). The former converts the value to an integer.

Number. The latter converts the value to a floating point number. These methods can be correctly run only when they are called for the String type; NaN is returned for other types.

1) parseInt ()

Before determining whether a string is a numeric value, parseInt () and parseFloat () will carefully analyze the string. The parseInt () method first checks the position 0

To determine whether it is a valid number. If not, the method returns NaN and does not continue to perform other operations. However, if this character is valid

Number. This method will view the characters at position 1 for the same test. This process continues until the characters with invalid numbers are found.

ParseInt () converts the string before this character to a number.

For example, if you want to convert the string "12345red" to an integer, parseInt () will return 12345 because when it checks the character r, it will stop

Stop the detection process.

The number literal in the string is correctly converted to a number, for example, "0xA" is correctly converted to a number 10. However, the string "22.5" will be

Convert to 22 because the decimal point is invalid for integers.

 

Var iNum1 = parseInt ("12345red"); var iNum2 = parseInt ("0xA"); var iNum3 = parseInt ("56.9"); var iNum4 = parseInt ("red "); document. write ("iNum1 =" + iNum1); // return the 12345 document. write ("iNum2 =" + iNum2); // return 10 documents. write ("iNum3 =" + iNum3); // return 56 documents. write ("iNum3 =" + iNum4); // return NaN

 

The parseInt () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer. Baseline

The second parameter of the parseInt () method is specified.

 

Var iNum1 = parseInt ("AF", 16); var iNum2 = parseInt ("10", 2); var iNum3 = parseInt ("10", 8 ); var iNum4 = parseInt ("10", 10); document. write ("iNum1 =" + iNum1); // return 175document. write ("iNum2 =" + iNum2); // 2document is returned. write ("iNum3 =" + iNum3); // return 8document. write ("iNum4 =" + iNum4); // return 10

 

2) parseFloat () method

The parseFloat () method is similar to the parseInt () method. You can view each character from position 0 until the first invalid character is found.

And then convert the string before the character to an integer. However, for this method, the first decimal point is a valid character. If there are two

Decimal point. The second decimal point is considered invalid. ParseFloat () converts the character before the decimal point to a number. This means the characters

The string "11.22.33" will be parsed to 11.22.
Another difference in using parseFloat () Is that a string must represent a floating point in decimal format, rather than octal or hexadecimal.

This method will ignore the leading 0, so the number of octal errors 0102 will be parsed to 102. For the hexadecimal number 0xA, this method returns NaN because

, X is not a valid character. In addition, the parseFloat () method does not have a base mode.

The following are examples of using parseFloat:

 

Var fNum1 = parseFloat ("12345red"); var fNum2 = parseFloat ("0xA"); var fNum3 = parseFloat ("11.2"); var fNum4 = parseFloat ("11.22.33 "); var fNum5 = parseFloat ("0102"); var fNum6 = parseFloat ("red"); document. write ("iNum1 =" + iNum1); // return 12345document. write ("iNum2 =" + iNum2); // return NaNdocument. write ("iNum3 =" + iNum3); // returns 11.2document.write ("iNum4 =" + iNum4); // returns 11.22document.write ("iNum5 =" + iNum5 ); // return 102document. write ("iNum6 =" + iNum6); // return NaN

 

(3) Forced type conversion

Use forced type conversion to process the type of the converted value. You can use forced type conversion to access a specific value, even if it is another type.

The three types of mandatory conversions available in ECMAScript are as follows:

1) Boolean (value)-converts a given value to a Boolean value;

2) Number (value)-converts a given value to a Number (which can be an integer or floating point Number );

3) String (value)-converts a given value to a String;

These should be well understood and can often be used when learning those advanced programming languages.

Four reference types

A reference type is usually called a class, that is, an object is processed when a reference value is encountered. In the traditional sense, ECMAScript does not really have

Class. In fact, except that there is no class, there is no "class" in the ECMA-262. ECMAScript defines "object definition", logic

It is equivalent to a class in other programming languages.

For details about JS objects, refer to: easy to learn JavaScript 9: JavaScript objects and arrays.

Let's take a look at instanceof, an operator that determines the reference type.

When using the typeof operator to store values of the reference type, a problem occurs. No matter what type of object is referenced, it returns "object ".

ECMAScript introduces another Java operator instanceof to solve this problem.

The instanceof operator is similar to the typeof operator and is used to identify the type of the object being processed. Different from the typeof method, instanceof

Developers are required to explicitly confirm that the object is of a specific type.

For example:

 

Var oStringObject = new String ("hello world"); document. write (oStringObject instanceof String); // output "true"
This code asks "is the variable oStringObject A String object instance ?" OStringObject is indeed a String object instance.

 

True ". Although it is not as flexible as the typeof method, the instanceof method is still useful when the typeof method returns "object ".

.
In addition, ECMAScript has a pseudo object, that is, other basic types. It can also be used as an object when new is used, for example:

String object, Boolean object, and Number object. They are reference types of basic types. For more information, see ECMAScript reference type.

ECMAScript also contains many objects, local objects, built-in objects and host objects. We will learn more about these in the next object-oriented approach.

5. Copy variable values

In terms of variable replication, the basic type and reference type are different. The basic type is to copy the value itself, while the reference type is to copy the address.

Let's look at the specific example:

 

Var box = "Lee"; var box2 = box; box2 = "Amy"; // After the value is re-assigned, the two basic types of variable operations do not affect each other, or they maintain their independent document. write ("box2 =" + box2 + ""); document. write ("box =" + box );

 

Output result: Amy

Lee

 

Var box = new Object (); box. name = "Lee"; var box2 = box; // copy the reference address value to box2box2. name = "Amy"; // After the value is re-assigned, both reference types point to the same object. If the name attribute changes, the original value is changed. Document. write ("box2.name =" + box2.name + ""); document. write ("box. name =" + box. name );
Output result: Amy
Amy

 

 

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.