Recently in the JavaScript Advanced program design, see before I think their JS learning is still good, and then, looked at a few pages feel like I did not learn the same, this mainly write I did not fully understand some of the knowledge.
The first is about the basic data type, starting from number, used to parseint to convert the value, read the book only to know that the purpose of this function is specifically used to convert the string into a numeric value, and did not know how it is a specific conversion rules. Let's take a look at the conversion rules for the number () function:
1. If it is a Boolean value, True and false are converted to 1 and 0, respectively.
2. If it is a numeric value, simply pass in and return.
3. If it is a null value, return 0.
4. If it is undefined, return nan.
5. If it is a string, follow these rules:
6. If the string contains only numbers (including cases preceded by a plus or minus sign), convert it to a decimal value, or "1"
will become 1, "123" will become 123, and "011" will become 11 (note: The leading 0 is ignored);
7. If the string contains a valid floating-point format, such as "1.1", it is converted to the corresponding floating-point value (again, the
slightly leading 0);
8. If the string contains a valid hexadecimal format, such as "0xf", convert it to a decimal integer of the same size
Numerical
9. If the string is empty (does not contain any characters), convert it to 0;
10. If the string contains characters other than the above format, convert it to Nan.
11. If it is an object, call the ValueOf () method of the object and then convert the returned value according to the previous rule. If the conversion
Result is Nan, the object's ToString () method is called, and the returned characters are converted again according to the previous rule
String value.
This rule is very well written, and let me know the specific operation of this function.
Then the string conversion.
There are two ways to convert a value to a string. The first is the ToString () method, which uses almost every value. The only way to do this is to return the string representation of the corresponding value.
For example: var num = 11;var str = true;
var numstr = num.tostring ()//value is "11";
var strStr = str.tostring ()//value is "true";
Numeric, Boolean, object, and string values have the ToString () method. However, the null and undefined values do not have this method.
The second method of conversion is the string () function. The conversion rules for this function are:
If the value has the ToString () method, the method (with no parameters) is called and the corresponding result is returned;
1. Returns "NULL" if the value is null;
2. If the value is undefined, return "undefined".
JavaScript Advanced Programming Note 1