Before getting into touch with every programming language, we first understand that our program design needs to process data, and the data is differentiated by the data type.
I. Data Types
Javascript is a weak scripting language. It has six data types and is divided into basic data types, special data types, and composite data types.
1. Basic data types: numeric, string, and Boolean
2. Special Data Types: null and undefined (the difference is that null requires explicit value assignment, while undefined indicates no value assignment)
3. Composite (reference) Data Type: Object (arrays are special objects)
Note: understand the differences between basic data types and reference data types. For example, function parameter transfer
Ii. Relationship between packages and basic data types
For basic data types, there are corresponding packaging classes (Object objects.
Number, String, Boolean
Note: basic data types are converted to basic type packaging objects under certain conditions.
The Code is as follows:
Var str = "this is a basic string ";
Var length = str. length ();
// When length () is used, the Javascript interpretation engine generates
// A temporary String object of str, Which is cleared after execution
Iii. How to Determine the Data Type
(1) typeof (chicken ribs)
Only the following six data types can be detected: number, string, boolean, undefined, object, and function (note !)
The Code is as follows:
Alert (typeof (null); // The result is an object.
Alert (typeof (a); // a is not assigned a value and the result is undefined.
Therefore, the basic data type can be determined as follows:
The Code is as follows:
Function type (o ){
Return (o = null )? 'Null': typeof (o );
}
(2) instanceof
However, for Composite data types (except functions), all objects are returned and cannot be determined by typeof.
You can use instanceof to check whether an object is an instance of another object. Note that the right operand of instanceof must be an object:
The Code is as follows:
Function Animal (){};
Function Pig (){};
Pig. prototype = new Animal ();
Alert (new Pig () instanceof Animal); // true
Instanceof is not suitable for detecting the type of an object.
(3) constructor
The Code is as follows:
Alert (1. constructor); // Error
Var o = 1;
Alert (o. constructor); // Number
O = null; // or undefined
Alert (o. constructor); // Error
Alert ({}. constructor); // Object
Alert (true. constructor); // Boolean
(4) Object. toString ()
The Code is as follows:
Function isArray (o ){
Return Object. prototype. toString. call (o) = '[object Array]';
}
Difference between call and apply:
They are all Function. prototype methods (for methods), which are implemented internally by the Javascript engine.
In fact, these two functions are almost the same. Note that the arg parameter in call (thisObj [, arg1 [, arg2 [,) can be a variable, the parameters in apply ([thisObj [, argArray]) are array sets.
The method is to lend a call to another object to complete the task. In principle, the context object changes during method execution.
(5) Summary
The Code is as follows:
Var _ toS = Object. prototype. toString,
_ Types = {
'Undefined': 'undefined ',
'Number': 'number ',
'Boolean': 'boolean ',
'String': 'string ',
'[Object Function]': 'function ',
'[Object RegExp]': 'regexp ',
'[Object Array]': 'array ',
'[Object Date]': 'date ',
'[Object Error]': 'error'
};
Function type (o ){
Return _ types [typeof o] | _ types [_ toS. call (o)] | (o? 'Object': 'null ');
}
Iv. Data Type Conversion
Javascript has two data types:
One is to convert the entire value from one type to another (called Basic data type conversion ),
Another method is to extract another type of value from one value and complete the conversion.
The following three methods are used to convert basic data types:
1. Convert to String type: String (); for example, the result of String (678) is "678"
2. Convert to numeric type: Number (); for example, the result of Number ("678") is 678.
3. Convert to Boolean: Boolean (); for example, Boolean ("aaa") returns true
The following method is used to extract another type of value from a value:
1. Extract the integer parseInt () in the string. For example, the result of parseInt ("123 zhang") is 123.
2. Extract the floating point: parseFloat () in the string. For example, the result of parseFloat ("0.55 zhang") is 0.55.
In addition, the methods for conversion of various types are summarized.
Number is converted into a String: String (number ),
Number. toString (2/8/16); // represents the binary octal hexadecimal default (No parameter,
ToFixed (3) // retain the third digit after the decimal point
ToExponextial (3); // the first digit of the decimal point. The third digit after the decimal point is var n = 123456.789; n. toExponextial (3); // 1.234e + 5, that is, 1.234X105
ToPrecision (4); // return the specified number of digits. If the number of digits is not fully displayed, the return value is in exponential notation (4 for each of the three methods)
5. Other Summary (something that is easy to ignore)
1. Trap of parseInt
The following part is taken from the essence of Javascript:
ParseInt is a function that converts a string to an integer. It stops parsing when it encounters a non-number, so parseInt ("16") and parseInt ("16 tons") generate the same result. If this function prompts that we have additional text, it won't.
If the first character of the string is 0, the string is evaluated based on octal instead of decimal. In October 8, 9 is not a number, so parseInt ("08") and parseInt ("09") generate 0 as the result. This error occurs when the program parses the date and time. Fortunately, parseInt can take a base number as the parameter, so the result of parseInt ("08", 10) is 8. I suggest you always provide this base parameter.
In addition. The following figure shows 1:
Alert (parseInt (0.0000001 ));
This is because js will use scientific notation to record numbers with a certain precision, for example:
Alert (0.0000001 );
It will get 1e-7, and parseInt will automatically convert the parameter into a string, which is actually:
The Code is as follows:
S = (1, 0.0000001). toString ();
Alert (parseInt (s ));
It is not surprising to get 1 at last.
When using parseInt, you must remember that the parameters are converted to strings before conversion.