Easy Learning JavaScript 11: JavaScript basic types (including type conversions) and reference types

Source: Internet
Author: User

type of a value

The basic type and reference type are mentioned when we introduce the data type of JS, but before we talk about the two types, let's take a look at the variables '

The type of the value. 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 stored directly in the location of the variable access.

(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 a primitive type or a reference type. To achieve this, the interpreter needs to

Attempts to determine whether the value is one of the basic types of ECMAScript, that is, the undefined type, the null type, the Boolean type, the number type, and the

The string type. Because the space occupied by these basic types is fixed, they can be stored in a smaller memory area-the stack. This storage facilitates

The value of the fast lookup variable.

In many languages, strings are treated as reference types, not primitive types, because the length of a string is variable. ECMAScript has broken this

Traditional.

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

, otherwise the speed of variable lookups is reduced. Instead, the value placed in the stack space of the variable is the address that the object stores 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:


two basic types

There are 5 basic types of ECMAScript, 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 its literal representation.

ECMAScript provides the 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 base type it represents.

Basic data types and operators typeof we also use them in front of blog post. For more information, refer to my blog: easy to learn

JavaScript five: JavaScript variables and data types.

three-type conversions

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 types to turn

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

In ECMAScript, type conversions are a short, one-step operation.

(1) Convert to String

The interesting thing about ECMAScript's original values of Boolean values, numbers, and strings is that they are pseudo-objects, which means that they actually have properties

and methods.

For example, to get the length of a string, you would use the following code:

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

Although "Red" is a basic type of string, it still has the property length, which is used to hold the size of the string. In summary, the 3 main primitive classes

Boolean values, numbers, and strings have the ToString () method to convert their values to strings. You might ask, "The string also has

ToString () method, is this not superfluous? "Yes, that's true, but ECMAScript defines that all objects have the ToString () method, whether it's pseudo

Object, or a true object. Because the string type belongs to a pseudo-object, it must have the ToString () method.

1) The ToString () method of the Boolean type just 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, namely the default mode and the base mode. Using the default mode, the ToString () method

Just use the corresponding string to output the numeric value (whether it's an integer, a floating-point number, or a scientific notation),

In the default mode, the ToString () method of number type returns the decimal table of numbers, regardless of what notation was originally used to declare a number

Shown Therefore, the output of a number declared in the form of eight hexadecimal literals is in decimal form. As shown below:

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

The base mode of the ToString () method with the number type can be used with different base output numbers, such as the binary base is 2, the octal base is 8,

The base of hexadecimal is 16.

Base is just another addition to the cardinality to be converted, which is the parameter of the ToString () method:

var iNum = 10;document.write (inum.tostring (2)),//Output "1010" document.write (inum.tostring (8));//Output "n" document.write ( Inum.tostring (16));//Output "a"

(2) Convert to Digital

ECMAScript provides two ways to convert non-numeric primitive values into numbers, namely parseint () and parsefloat (). The former converts the value into an integer

Number, which converts the value to a floating point. These methods are called only for string types, and are returned as Nan for the other types.

1) parseint ()

parseint () and parsefloat () will parse the string carefully before judging whether the string is a numeric value. The parseint () method first looks at location 0

To determine if it is a valid number, and if it is not, the method returns Nan, and no further action is taken. But if the character is valid

Number, the method will look at the character at position 1 and perform 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 () will return 12345, because when it checks to the character R, it stops

Detection process.

the numeric literals contained in the string are converted correctly to numbers, such as "0xA" being correctly converted to the number 10. However, the string "22.5" will be

Converted to 22 because the decimal point is an invalid character for integers.

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 to document.write ("inum3=" +inum4);//Return Nan

The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into integers. Base is made up of

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

var = parseint ("AF", INum1), var iNum2 = parseint ("Ten", 2), var iNum3 = parseint ("Ten", 8), var iNum4 = parseint ("10", 10); document.write ("inum1=" +inum1);//Return 175document.write ("inum2=" +inum2);//Return to 2document.write ("inum3=" +iNum3);// Return 8document.write ("inum4=" +inum4);//Return 10

2) parsefloat () method

The Parsefloat () method is similar to the parseint () method, viewing each character starting at position 0 until the first non-valid word is found identifier

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

Decimal points, the second decimal point will be considered invalid. Parsefloat () converts the character before the decimal point to a number. This means that the character

The string "11.22.33" will be parsed into 11.22.
Another difference between using the Parsefloat () method is that the string must represent a floating-point number in decimal form, rather than octal or hexadecimal.

The method ignores the leading 0, so the octal number 0102 is parsed to 102. For hexadecimal 0xA, the method returns NaN because the floating-point number

, x is not a valid character. In addition, the parsefloat () method does not have a base pattern.

Here 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 ");d ocument.write (" inum1= "+inum1);// Return 12345document.write ("inum2=" +inum2);//Return Nandocument.write ("inum3=" +inum3);//Return 11.2document.write ("iNum4=" + INUM4);//Return 11.22document.write ("inum5=" +INUM5);//Return to 102document.write ("inum6=" +INUM6);//Return Nan

(3) Forced type conversion

Use the coercion type conversion to handle the type of the converted value. A specific value can be accessed using a coercion type conversion, even if it is of a different type.

The 3 types of coercion conversions available in ECMAScript are as follows:

1) Boolean-converts the given value to a Boolean type;

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

3) string (value)-converts the given values to a string;

These should be well understood and often used when learning high-level programming languages.

Four reference types

reference types are often called classes, and that is, the reference value is encountered and the object is processed. In the traditional sense, ECMAScript is not really

there are classes. In fact, in addition to stating that there is no class, the word "class" does not appear in ECMA-262. ECMAScript defines the "Object definition", the logical

Classes in other programming languages.

A detailed explanation of the JS object is also available in the previous blog post, refer to: Easy learning JavaScript nine: JavaScript objects and arrays.

Let's look at an operator that determines the reference type instanceof

Storing a value with a reference type when using the TypeOf operator will cause a problem, regardless of what type of object is referenced, and it returns "Object".

ECMAScript introduced another Java operator, instanceof, to solve this problem.

The instanceof operator is similar to the typeof operator to identify the type of object being processed. Unlike the TypeOf method, the instanceof side

Method requires the developer to explicitly confirm that the object is of a particular type.

For example:

var ostringobject = new String ("Hello World");d Ocument.write (Ostringobject instanceof string);//Output "true"
This code asks "is the variable ostringobject an instance of a String object?" "Ostringobject is indeed an instance of a string object, so the knot

The result is "true". Although not as flexible as the TypeOf method, the Instanceof method is useful in cases where the TypeOf method returns "Object"

Of
In addition, ECMAScript also has pseudo-objects, which are other basic types that can be used as objects when created with new, such as:

A String object, a Boolean object, and a number object. They are reference types of the base type. Learn more about the reference: ECMAScript reference type.

ECMAScript also contains many objects, local objects, built-in objects, and host objects. This is what we'll look at later in the object-oriented context.

Five copy variable values

In the context of variable copying, the base type differs from the reference type in that the base type is the copy of the value itself, and the reference type replicates the address.

Let's look at specific examples:

var box= "Lee"; var Box2=box; Box2= "Amy";//After re-assignment, the two basic type variables operate independently of each other, or remain independent document.write ("box2=" +box2+ "<br/>"); document.write ("box=" +box);

The result of the output is: Amy

Lee


Var box=new object (); Box.name= "Lee"; var box2=box;//copy the reference address value to Box2box2.name= "Amy";//After re-assignment, two reference types point to the same object. The Name property changes the original value whenever a change occurs. document.write ("box2.name=" +box2.name+ "<br/>");d ocument.write ("box.name=" +box.name);
The result of the output is: Amy
Amy

Easy Learning JavaScript 11: JavaScript basic types (including type conversions) and reference types

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.