Home>

First, the type of value

Earlier in the introduction of the data types of js mentioned the basic types and reference types,But before talking about the two types,Let's first understand the type of the value of the variable.In ecmascript, variables can have two types of values,That is, the original value and the reference value.

(1) Original value

Simple data segments stored on the stack,That is,Their values ​​are stored directly where the variable is accessed.

(2) Reference value

Objects stored in the heap,That is,The value stored in the variable is a pointer,Point to the memory where the object is stored.

When assigning values ​​to variables,The ecmascript interpreter must determine that the value is a primitive type,Still reference types.To achieve this,The interpreter needs to try to determine if the value is one of the basic types of ecmascript,Namely undefined type, null type, boolean type, number type and string type. Since the space occupied by these basic types is fixed,So they can be stored in a smaller memory area-the stack. This storage makes it easy to quickly find the value of a variable.

In many languages,Strings are considered reference types,Not basic types,Because the length of the string is variable.ecmascript breaks this

Tradition.

If a value is of reference type,Then its storage space will be allocated from the heap.Since the size of the reference value changes,So you can't put it on the stack,Otherwise it will reduce the speed of variable search.Instead, the value placed in the variable's stack space is the address where the object is stored on the heap.The size of the address is fixed,So storing it on the stack has no negative impact on variable performance.As shown below:

Second, the basic types

ecmascript has 5 basic types,Namely undefined type, null type, boolean type, number type and string type. ecma-262 defines the term type as a collection of values,Each primitive type defines the range of values ​​it contains and its 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 primitive type:if it is a primitive type,You can also determine which basic type it represents.

The basic data types and operator typeof are also frequently used in previous blog posts.For more details, please refer to this article:Detailed JavaScript variables and data types.

Third, type conversion

One of the most important characteristics 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 conversions,There are also several global methods that can be used for more complex transformations.In either case,In ecmascript, type conversion is a short one-step operation.

(1) Convert to string

The ecmascript boolean value, the original value of numbers and strings is interesting because they are pseudo objects,This means that 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 basic type string,It still has the property length, which holds the size of the string.all in all,The three main primitive types boolean values, numbers, and strings all have tostring () methods that convert their values ​​to strings.You might ask,"Is there a tostring () method for strings, isn't this redundant?" Yes, it is,However, ecmascript defines that all objects have a tostring () method, whether it is a pseudo object,Still true objects.Because the string type is a pseudo object,So it must have a tostring () method.

1) The boolean tostring () method 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 is special,It has two modes,That is, the default mode and the base mode.In the default mode,The tostring () method simply outputs a numeric value (whether it is an integer, floating point number, or scientific notation) with the corresponding string. In the default mode,No matter what notation is initially used to declare the number,The tostring () method of type number returns the decimal representation of the number.Therefore, numbers declared as octal or hexadecimal literals 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"

Base mode using the tostring () method of type number,Numbers can be output in different bases,For example, the base of binary is 2, the base of octal is 8, and the base of hexadecimal is 16.

The base is just another addition to the base to be converted,It is a parameter to 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 "a"

(2) Convert to numbers

ecmascript provides two ways to convert non-numeric primitive values ​​into numbers.That is, parseint () and parsefloat (). The former converts values ​​to integers,The latter converts the value to a floating point number.These methods are only called on string types,In order to run correctly;All other types return nan.

1) parseint ()

Before determining whether a string is a numeric value,Both parseint () and parsefloat () parse the string carefully.The parseint () method first looks at the character at position 0,Determine whether it is a valid number;if not,The method will return nan and no further operations will be performed.But if the character is a valid number,This method will look at the character at position 1,Perform the same test.This process continues until non-significant characters are found,At this point parseint () will convert the string before the character into a number.

For example, if i want to convert the string "12345red" to an integer,Then parseint () will return 12345 because when it detects the character r, it stops the detection process.

Numeric literals contained in strings are correctly converted to numbers,For example, "0xa" will be correctly converted to the number 10. However, the string "22.5" will be converted to 22 because for integers,The decimal point is an invalid character.

var inum1=parseint ("12345red");
var inum2=parseint ("0xa");
var inum3=parseint ("56.9");
var inum4=parseint ("red");
document.write ("inum1 =" + inum1);//Returns 12345
document.write ("inum2 =" + inum2);//Returns 10
document.write ("inum3 =" + inum3);//return 56
document.write ("inum3 =" + inum4);//return nan

The parseint () method also has a base mode,You can convert a binary, octal, hexadecimal, or any other base 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);//Returns 175
document.write ("inum2 =" + inum2);//return 2
document.write ("inum3 =" + inum3);//return 8
document.write ("inum4 =" + inum4);//Returns 10

2) parsefloat () method

The parsefloat () method is similar to the parseint () method.Look at each character starting at position 0,Until the first non-valid character is found,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 there are two decimal points,The second decimal point will be considered invalid.parsefloat () will convert the characters before this decimal point into numbers.This means that the string "11.22.33" will be parsed into 11.22.

Another difference with the parsefloat () method is thatThe string must represent a floating point number in decimal form.Instead of using octal or hexadecimal.This method ignores leading zeros, so the octal number 0102 is parsed to 102. For hexadecimal number 0xa, this method will return nan, because in floating-point numbers,x is not a valid character.Also, the parsefloat () method has no base mode.

Here are some examples 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);//Returns 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);//Returns 102
document.write ("inum6 =" + inum6);//return nan

(3) Casting

Use casts to handle the type of the converted value.Use casts to access specific values,Even if it's another type.The three casts available in ecmascript are as follows:

1) boolean (value)-converts the given value to a boolean type; 2) number (value)-convert the given value into a number (can be an integer or a floating point number); 3) string (value)-convert the given value into a string;

These should be well understood,These are often used when learning those high-level programming languages.

Fourth, reference types

Reference types are often called classes,That is,Encountered a reference value,All that is processed is the object.In the traditional sense,ecmascript does not really have classes.In fact, apart from stating that classes do not exist,The word "class" does not appear at all in ecma-262. ecmascript defines "object definition", which is logically equivalent to classes in other programming languages.

Let's take a look at an instanceof operator that judges the reference type. When using the typeof operator, there is a problem in storing values ​​by reference typeNo matter what type of object is being referenced,It all returns "object". ecmascript introduces another java operator instanceof to solve this problem.The instanceof operator is similar to the typeof operator,Used to identify the type of object being processed.Unlike the typeof method,instanceof party

The law requires developers to explicitly identify objects as a certain type.

E.g:

var ostringobject=new string ("hello world");
document.write (ostringobject instanceof string);//output "true"

This code asks "Is the variable ostringobject an instance of a string object?"So the result is "true". Although not as flexible as the typeof method,But in the case where the typeof method returns "object",The instanceof method is still useful.

Five, copy the variable valueIn terms of variable replication,Basic types and reference types are different,The basic type is the value itself,Reference types copy addresses.

Let's look at specific examples:

var box="lee";
var box2=box;
box2="amy";//After reassignment,Two basic types of variables do not affect each other when operating,Or keep their independence
document.write ("box2 =" + box2 + "<br />");
document.write ("box =" + box);

The output is:amy

lee

var box=new object ();
box.name="lee";
var box2=box;//Copy the reference address value to box2
box2.name="amy";//After reassignment,Both reference types point to the same object.
The name property will change the original value whenever it changes.
document.write ("box2.name =" + box2.name + "<br />");
document.write ("box.name =" + box.name);

The output is:amy

amy

  • Previous JavaScript programming JS debugging
  • Next Basic tutorial on triggers in MySQL