Explain JavaScript basic types and reference types _javascript tips

Source: Internet
Author: User
Tags numeric value

The type of a value
The basic types and reference types are mentioned earlier in the introduction of the data types of JS, but before we can say two types, let's look at the type of the variable's value. In ECMAScript, a variable can have two types of values, namely the original value and the reference value.
(1) Original value
Simple data segments stored on the stack, that is, their values are stored directly in the location where the variable is accessed.
(2) Reference value
The object stored in the heap, that is, the value stored at the variable is a pointer to the memory where the object is stored.
When assigning a value to a variable, the ECMAScript interpreter must determine whether the value is of the original type or a reference type. To do this, the interpreter needs to try to determine whether the value is one of the basic types of ECMAScript, namely the undefined type, the null type, the Boolean type, the number type, and the string type. Because these basic types occupy a fixed amount of space, they can be stored in smaller memory areas-stacks. This stores the values that make it easy to search for variables quickly.
In many languages, strings are treated as reference types, not basic types, because the length of strings is variable. The ECMAScript broke the
Traditional.
If a value is a reference type, its storage space is allocated from the heap. Because the size of the reference value will change, you cannot put it on the stack, otherwise it will reduce the speed of the variable lookup. Instead, the value placed in the stack space of the variable is the address that the object is stored in the heap. The size of the address is fixed, so storing it in the stack has no negative effect on the performance of the variable. As shown in the following illustration:

II. Basic Types
ECMAScript has 5 basic types, namely the undefined type, the null type, the Boolean type, the number type, and the string type. ECMA-262 defines the term type as a collection of values, each of which defines the range of values it contains and their literal representation.
ECMAScript provides a typeof operator to determine whether a value is within a certain type of range. 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.
Basic data types and operators typeof we are also used frequently in front of the posting. For more information, you can refer to this article: detailed JavaScript variables and data types .

Third, type conversion
One of the most important features of all programming languages is the ability to perform type conversions. ECMAScript provides developers with a large number of simple type conversion methods. Most types have methods for simple transformations, and several global methods can be used for more complex transformations. In either case, type conversion is a short step in ECMAScript.
(1) Convert to String
The interesting thing about the ECMAScript of Boolean values, numbers, and the original values of strings is that they are pseudo objects, which means they actually have properties and methods.
For example, to get the length of a string, you can use the following code:

var sbox = "Red"; 
document.write (sbox.length);//Output 3 

Although "Red" is a primitive type of string, it still has attribute length, which holds the size of the string. All in all, 3 of the main primitive types Boolean values, numbers, and strings have the ToString () method, which converts their values to strings. You might ask, "Does the string have the ToString () method, is it unnecessary?" "Yes, it does, but ECMAScript defines that all objects have a ToString () method, whether it's a pseudo object or a true object. Because the string type is a pseudo object, it must have the ToString () method.
1 The Boolean-type ToString () method simply 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 number type is special, it has two modes, that is, the default mode and the base mode. With the default mode, the ToString () method simply outputs a numeric value (whether integer, floating point, or scientific notation) in the corresponding string, and in the default mode, regardless of the initial notation used to declare the number, the number type ToString () method returns a decimal representation of the digits. Therefore, the numeric output declared in eight-or hexadecimal-literal form is in decimal form. As shown below:

var iNum1 = ten; 
var iNum2 = 10.0; 
document.write (Inum1.tostring ());//Output "10" 

Using the base pattern of the number type toString () method, you can use different base output numbers, for example, the binary base is 2, the octal base is 8, and the hexadecimal base is 16.
The base is just another addition to the cardinality to be converted, which is the parameter of the ToString () method:

var inum = ten; 
document.write (inum.tostring (2));//Output "1010" 
document.write (inum.tostring (8));//Output "a" 
document.write ( Inum.tostring (16));//Output "a" 

(2) Convert to number
       ECMAScript provides two methods for converting a non-numeric original value into a number, namely parseint () and parsefloat (). The former converts the value to an integer, and the latter converts the value to a floating-point number. These methods are invoked only on string types, and they are run correctly, and Nan is returned for other types.
1) parseint ()
       parseint () and parsefloat () parse the string carefully before determining whether the string is a numeric value. The parseint () method first looks at the character at position 0 to determine whether it is a valid number, and if not, the method returns Nan and does not continue with other operations. However, if the character is a valid number, the method will look at the characters at position 1 and do the same test. This process continues until a character is found that is not a valid number, at which point parseint () converts the string before the character to a number.
       For example, if you want to convert the string "12345red" to an integer, then parseint () returns 12345, because when it checks the character R, it stops the detection process. The number literal that is contained in the
       string is converted to numbers correctly, such as "0xA" converted to the Number 10. However, the string "22.5" will be converted to 22 because the decimal point is an invalid character for an integer.

var iNum1 = parseint ("12345red"); 
var iNum2 = parseint ("0xA"); 
var iNum3 = parseint ("56.9"); 
var iNum4 = parseint ("red"); 
document.write ("inum1=" +inum1);/return 12345 
document.write ("inum2=" +inum2);//Return to 
document.write ("inum3=" +INUM3);/return 56 

The parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string of strings into integers. The base is specified by the second parameter of the parseint () method.

var iNum1 = parseint ("AF"); 
var iNum2 = parseint ("Ten", 2); 
var iNum3 = parseint ("Ten", 8); 
var iNum4 = parseint ("ten"); 
document.write ("inum1=" +inum1);/return 175 
document.write ("inum2=" +inum2);/return 2 
document.write ("inum3=" + INUM3);/return 8 
document.write ("inum4=" +inum4);/return 10 

The

2) parsefloat () method
        parsefloat () method is similar to how the parseint () method is handled. View each character starting at position 0 until you find the first one that is not valid, and then convert the string before the character to an integer. However, for this method, the first decimal point that appears is a valid character. If you have two decimal points, the second decimal point is considered invalid. Parsefloat () converts the characters before the decimal point to numbers. This means that the string "11.22.33" will be parsed into 11.22. Another difference in
       using the Parsefloat () method is that the string must represent floating-point numbers in decimal form, not octal or hexadecimal. The method ignores leading 0, so the octal number 0102 will be resolved to 102. For hexadecimal 0xA, the method returns NaN, because x is not a valid character in a floating-point number. Also, the parsefloat () method does not have a base pattern.
      Below are some examples of using the Parsefloat () method:

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 12345 
document.write ("inum2=" +inum2);//Return nan 
document.write ("inum3= "+inum3);/return 11.2 
document.write (" inum4= "+inum4);//Return 11.22 document.write 
(" inum5= "+INUM5);/return 102 
document.write ("inum6=" +INUM6);//Return Nan 

(3) Coercion type conversion
Use coercion type conversion to handle the type of the converted value. You can use coercion type conversions to access a specific value, even if it is of another type. The 3 mandatory type conversions available in ECMAScript are as follows:

    • 1 Boolean (value)-Converts a given value to a Boolean;
    • 2 Number (value)-Converts a given value to a digit (can be an integer or floating point);
    • 3 string (value)-converts the given value to a string;

These should be well understood and often used when learning advanced programming languages.
Four, reference type
A reference type is usually called a class, that is, when a reference value is encountered, the object is handled. In the traditional sense, ECMAScript does not really have classes. In fact, in addition to stating that there is no class, the word "class" does not appear in the ECMA-262. ECMAScript defines an "object definition" that is logically equivalent to a class in another programming language.
A detailed explanation of JS objects is also available in the previous posting, for reference: Easy learning JavaScript nine: JavaScript objects and arrays.
Let's take a look at an operator instanceof that determines a reference type, and when you use the TypeOf operator to store a value with a reference type, a problem arises, regardless of what type of object is referenced, it returns "Object". ECMAScript introduced another Java operator instanceof to solve the problem. The instanceof operator is similar to the typeof operator and is used to identify the type of object being processed. Unlike the TypeOf method, the instanceof side
The method requires the developer to explicitly confirm that the object is a specific type.
For example:

var ostringobject = new String ("Hello World"); 
document.write (Ostringobject instanceof String);//Output "true" 

The code asks, "is the variable ostringobject an instance of a String object?" "Ostringobject is indeed an instance of a string object, so the result is" true ". Although not as flexible as the TypeOf method, the Instanceof method is useful when the TypeOf method returns "Object".
In addition, ECMAScript also has pseudo objects, that is, other basic types that can be used as objects when created using new, such as String objects, Boolean objects, and number objects. They are reference types of the base type. Learn more about references: ECMAScript reference types. ECMAScript also contains a number of objects, local objects, built-in objects, and host objects. These will be specifically understood in the later object-oriented.
v. Copy the value of a variable
in the context of variable replication, the base type differs from the reference type in that the base type is copied by the value itself, whereas the reference type copies the address.
Let's look at specific examples:

var box= "Lee"; 
var Box2=box; 
Box2= "Amy"; After the assignment, two basic types of variables operate independently of each other, or maintain their respective independence 
document.write ("box2=" +box2+ "<br/>"); 

The result of the output is: Amy
Lee

var box=new Object (); 
Box.name= "Lee"; 
var box2=box;//copies the reference address value to Box2 
box2.name= "Amy";//The two reference types point to the same object after the assignment. The Name property changes the original value whenever a change occurs. 
document.write ("box2.name=" +box2.name+ "<br/>"); 
document.write ("box.name=" +box.name); 

The result of the output is: Amy
Amy

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

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.