Basic JavaScript types and reference type_javascript skills

Source: Internet
Author: User
This article mainly introduces the basic types and reference types of JavaScript. The basic types also include type conversion. If you are interested, refer I. Value Type
We mentioned the basic and reference types as early as we introduced the JS data types. But before talking about the two types, let's take a look at the types of variable values. 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 to determine whether the value is one of the basic types of ECMAScript, namely, 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 storage makes it easy to quickly search for variable values.
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 in the stack. Otherwise, the variable search speed will be 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:

Ii. Basic Types
ECMAScript has five basic types: Undefined, Null, Boolean, Number, and String. The ECMA-262 defines the term type as a set of values, each of which 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 details, refer to this article:Describes the JavaScript variables and Data Types.

Iii. type conversion
One of the most important features of all programming languages is the ability to perform type conversion. ECMAScript provides developers with a large number of simple type conversion methods. Most types have simple conversion methods, and several global methods can be used for more complex conversions. In either case, type conversion is a brief step in ECMAScript.
(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 attributes 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, the three main primitive types of Boolean values, numbers, and strings all have toString () methods, which can be converted to strings. You may ask, "Is there a toString () method in the string? Isn't that redundant ?" 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. The default mode is used. The toString () method only outputs numeric values using the corresponding string (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 the decimal representation of the Number. 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, and 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 character at 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 the character is a valid number, this method will view the characters at position 1 and perform the same test. This process continues until a non-valid number is found. At this time, parseInt () converts the string before this character into a number.
For example, if you want to convert the string "12345red" to an integer, parseInt () returns 12345 because the detection process stops when the character r is checked.
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" is converted 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. The base is specified by the second parameter of the parseInt () method.

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 the 175 document. write ("iNum2 =" + iNum2); // 2 document is returned. write ("iNum3 =" + iNum3); // return 8 documents. write ("iNum4 =" + iNum4); // return 10

2) parseFloat () method
The parseFloat () method is similar to the parseInt () method. Each character is viewed from position 0 until the first invalid character is found, then, convert the string before the character to an integer. However, for this method, the first decimal point is a valid character. If two decimal points exist, the second decimal point is considered invalid. ParseFloat () converts the character before the decimal point to a number. This means that 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 the floating point number. 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 the 12345 document. write ("iNum2 =" + iNum2); // return NaN document. write ("iNum3 =" + iNum3); // return the 11.2 document. write ("iNum4 =" + iNum4); // return the 11.22 document. write ("iNum5 =" + iNum5); // return 102 document. 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.
Iv. Reference Type
A reference type is usually called a class, that is, an object is processed when a reference value is encountered. Traditionally, ECMAScript does not really have classes. In fact, except that there is no class, there is no "class" in the ECMA-262. ECMAScript defines "object definitions", which are logically equivalent to classes 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 used to determine the reference type. When using the typeof operator, a problem occurs when using the reference type storage value. 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, so the result is "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 also has a pseudo object, that is, other basic types. When new is used, it can also be used as an object, such as a String object, a Boolean object, and a 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.
V. 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 box2 box2.name = "Amy"; // After the value is assigned again, 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

The above is a detailed introduction to the basic types and reference types of JavaScript, and I hope to help you learn it.

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.