This article mainly introduces the parsing of rule instances for Javascript type conversion, involving knowledge about javascript type conversion. If you are interested in this article, let's learn about it. type conversion can be divided into implicit conversion and explicit conversion, implicit conversions are the automatic conversions performed by the program at runtime. Explicit conversions are manually forced conversions of types. Javascript variables are loose types. They can store any data types supported by Javascript, and their variable types can be dynamically changed at runtime. See
Example:
var n = 10;n = "hello CSSer!";n = {};
In the above example, first declare the n variable and initialize its value to 10 (integer type), then the string "hello CSSer !" Assign a value to n, and then assign another object to it. The last n type is the object type. We can see that the type of Variable n is dynamic. In actual programming, we recommend that you do not change the type of the variable frequently, because this is not good for debugging.
Because the variable types in Javascript are dynamic, the concept of type conversion is required during actual execution of the program. Implicit conversions can be divided into implicit conversions and Explicit conversions. Implicit conversions are the automatic conversions performed by the program at runtime. Explicit conversions are manually forced conversions of types. This article will summarize the type conversion of Javascript.
Explicit Conversion
By manually performing type conversion, Javascript provides the following Transformation functions:
Convert to numeric type: Number (mix), parseInt (string, radix), parseFloat (string)
Convert to String type: toString (radix), String (mix)
Convert to Boolean type: Boolean (mix)
1. Number (mix) function, which can convert any type of parameter mix to numeric type. The rules are as follows:
1. If it is a Boolean value, true and false are converted to 1 and 0 respectively.
2. If it is a numeric value, return itself.
3. If it is null, 0 is returned.
4. If it is undefined, NaN is returned.
5. If it is a string, follow the following rules:
6.
1. If the string contains only numbers, convert it to decimal (ignore leading 0)
2. If the string contains a valid floating point format, convert it to a floating point value (ignore leading 0)
3. If it is a Null String, convert it to 0
4. If the string contains a non-preceding format, convert it to NaN
7. If it is an object, call the valueOf () method of the object and convert the returned value according to the previous rule. If the conversion result is NaN, The toString () method of the object is called and the returned string value is converted again according to the preceding rules.
The following table lists the returned values of the valueOf () object:
Object |
Return Value |
Array |
Elements of the array are converted into strings separated by commas (,) and connected together. Its operations are the same as those of Array. toString and Array. join. |
Boolean |
Boolean value. |
Date |
The storage time is the number of milliseconds from midnight, January 1, January 1, 1970 in UTC. |
Function |
Function itself. |
Number |
Numeric value. |
Object |
Object. This is the default situation. |
String |
String value. |
Here are a few examples. Can you write out the correct results:
Number("hello CSSer!");//NaNNumber("0x8");//8Number("");//0Number("020dd");//NaNNumber("070");//70Number(true);//1
2. parseInt (string, radix) function, which converts a string to an integer value. It also has certain rules:
1. Ignore the spaces before the string until the first non-null character is found.
2. If the first character is not a digit or a negative number, NaN is returned.
3. If the first character is a number, the parsing continues until the string is parsed or a non-numeric symbol is encountered.
4. If the parsing result in the previous step starts with 0, it will be parsed as an octal component. If it starts with 0x, it will be parsed as a hexadecimal component.
Object |
Operation |
Array |
Converts an Array element to a string. The result string is separated by commas and connected. |
Boolean |
If the Boolean value is true, "true" is returned ". Otherwise, "false" is returned ". |
Date |
Returns the text representation of a date. |
Error |
Returns a string containing the error message. |
Function |
Returns a string in the following format, where functionname is the name of the toString method function called: Function functionname () {[native code]} |
Number |
Returns the text representation of a number. |
String |
Returns the value of a String object. |
Default |
"[Object objectname]" is returned, where objectname is the object type name. |
5. If the radix parameter is specified, the base is radix.
Quiz:
parseInt("hello CSSer!");//NaNparseInt("0x8");//8parseInt("");//NaNparseInt("020dd");//20parseInt("070");//70parseInt("22.5");//22
3. parseFloat (string) function, which converts a string to a value of the floating point type.
Its rules are basically the same as parseInt, but there is also a difference: the first decimal point in the string is valid, and parseFloat ignores all leading 0, if a string contains a number that can be parsed as an integer, the return value is an integer instead of a floating point value.
4. toString (radix) method. All types of values except undefined and null have the toString () method, which is used to return the string representation of the object.
5. String (mix) function, which converts any type of value to a String. The rule is:
1. If the toString () method exists, call this method (without passing the radix parameter) and return the result.
2. If it is null, return "null"
3. If it is undefined, return "undefined"
6. the Boolean (mix) function converts any type of value to a Boolean value.
The following values are converted to false: false, "", 0, NaN, null, and undefined. All other values are converted to true.
Implicit conversion
In some cases, Javascript performs automatic type conversion even if we do not provide display conversion. The main situations include:
1. function used to check whether it is a non-numeric value: isNaN (mix)
IsNaN () function, tested and found that this function will try to convert the parameter value with Number (). If the result is "non-numeric", true is returned; otherwise, false is returned.
2. incrementing and decreasing Operators (including front and back) and unary plus/Negative Operators
These operators apply to values of any data type. For values of different types, the operators follow the following rules (the Rules are basically the same as those of Number ):
1. If a string contains valid numeric characters, convert it to a numeric value (the conversion rule is the same as Number (). After adding or subtracting 1, the string variable is changed to a numeric variable.
2. If a string does not contain valid numeric characters, set the value of the variable to NaN and convert the string variable to a numeric variable.
3. If it is a Boolean value of false, first convert it to 0 and then perform the operation of adding or subtracting 1. The Boolean value variable is a numerical variable.
4. If the Boolean value is true, convert it to 1 first, and then add or subtract 1. The Boolean value variable becomes a numerical variable.
5. If it is a floating point value, add or subtract 1.
6. If it is an object, first call the valueOf () method of the object, and then apply the previous rule to the returned value. If the result is NaN, call the toString () method and then apply the preceding rule. The object variable is a numeric variable.
Quiz:
What is the result of performing the post-increment operation on the following types of values?
"2", "02dd", "", false, 22.5, + "",-false, + new Date ()
3. Addition Operators
The plus operator is also used for string connectors in Javascript, so the plus operator rules are divided into two situations:
• If both operation values are numerical values, the rule is:
1. If an operand is NaN, the result is NaN.
2. If it is Infinity + Infinity, the result is Infinity.
3. If it is-Infinity + (-Infinity), the result is-Infinity.
4. If it is Infinity + (-Infinity), the result is NaN.
5. If it is + 0 + (+ 0), the result is + 0
6. If it is (-0) + (-0), the result is-0.
7. If it is (+ 0) + (-0), the result is + 0.
• If an operation value is a string:
1. If both operation values are strings, splice them.
2. If only one operation value is a string, convert another operation value to a string and splice it.
3. If an operand is an object, value, or Boolean value, call the toString () method to obtain the string value, and then apply the preceding string rule. For
Undefined and null, respectively, call String () to explicitly convert to String.
It can be seen that in addition operations, if one operation value is of the string type, the other operation value is converted to a string and then connected.
4. multiplication, division, subtraction, and modulo Operators
These operators are intended for computation, so they are common: if one of the Operation values is not a numerical value, the Number () function is implicitly called for conversion. For detailed rules for each operation, see definitions in ECMAScript.
5. logical operators (! , &, |)
Non-logical (!) The operator first converts its operation value to a Boolean value through the Boolean () function, and then reverse it.
Logic and (&) operators. If an operation value is not a Boolean value, follow the following rules for conversion:
1. If the first operand is converted to true by Boolean (), the second operation value is returned. Otherwise, the first operation value (not the value after Boolean () conversion) is returned)
2. If one operation is null, null is returned.
3. If an operation value is NaN, NaN is returned.
4. If an operation value is undefined, undefined is returned.
Logical or (|) operator. If an operation value is not a Boolean value, follow the following rules:
1. If the first operation value is converted to false after Boolean (), the second operation value is returned. Otherwise, the first operation value (not the value after Boolean () conversion) is returned)
2. The processing rules and logic of undefined, null, and NaN are the same as those (&).
6. Relational operators (<, >,< =,> =)
Like the preceding operators, the Operation values of Relational operators can be of any type. Therefore, when using non-numeric types for comparison, implicit type conversion is also required:
1. If both operation values are numerical values, compare them.
2. If both operation values are strings, compare the character encoding values corresponding to the strings.
3. If only one operation value is a value, convert the other operation value to a value for numerical comparison.
4. If an operand is an object, call the valueOf () method (if the object does not have the valueOf () method, call the toString () method). The result is as follows:
Rule execution comparison
5. If an operation value is a Boolean value, convert it to a numerical value before comparison.
Note: NaN is a very special value, which is not equal to any type of value, including itself, and returns false if it is larger than any type of value.
7. Equal operator (=)
The equal operator implicitly converts the operation values and then compares them:
1. If an operation value is a Boolean value, convert it to a value before comparison.
2. If one operation value is a string and the other operation value is a numerical value, the Number () function is used to convert the string to a numerical value.
3. If one operation value is an object and the other is not, call the valueOf () method of the object and compare the result according to the preceding rules.
4. null and undefined are equal.
5. If an operation value is NaN, equal comparison returns false.
6. If both operation values are objects, compare whether they point to the same object.