JS type conversion very disgusting, we all know, but the study is still to learn.
See Rhino Book today see the conversion rules, summed up.
X-to-string, numeric, Boolean
x represents various types of values, directly:
value |
Turn Numbers |
go to String |
Turn Boolean value |
Undefined |
NaN |
"Undefined" |
False |
Null |
0 |
"NULL" |
False |
True |
1 |
"True" |
|
False |
0 |
"False" |
|
0 |
|
"0" |
False |
-0 |
|
"0" |
False |
NaN |
|
"NaN" |
False |
Infinity |
|
"Infinity" |
True |
-infinity |
|
"-infinity" |
True |
1 (not 0) |
|
"1" |
True |
{} (Any object) |
See below |
See below |
True |
[] (any array) |
0 |
”” |
True |
[9] (contains a numeric element) |
9 |
"9" |
True |
["A"] (Other arrays) |
NaN |
Use the. Join () method |
True |
function () {} (any function) |
NaN |
See below |
True |
Two basic methods
There are three "see below" above, that is, see here. These three places are the most troublesome place for JS conversion.
First, let's look at two basic conversion methods:
toString() // 返回对象的字符串表示形式valueOf() // 返回对象的数字表示形式
At first glance is very simple, don't worry, the key is that JS will not be as simple as you want to call these methods.
Object to String
The steps are as follows:
- If you have the ToString () method, call the ToString () method of the object, and if you return a literal, convert the literal to a string and return it. The literal turn string is shown in the table above.
- If there is no ToString () method or no return literal, and there is a valueof () method, call the ValueOf () method. If this method returns a literal, it is converted into a string return.
- If neither of these steps succeeds, a type error exception is thrown.
object to Number
In fact, the object is changed to the first two steps of the string, and then the results are converted into numbers.
The steps are as follows:
- If there is a valueof () method, call the ValueOf () method. If this method returns a literal, it is converted to a numeric return.
- If there is no valueof () method, or there is no return literal, and the ToString () method is called, the ToString () method of the object is invoked, and if a literal is returned, the literal is converted to a number and returned. The literal turn figures are shown in the table above.
- If neither of these steps succeeds, a type error exception is thrown.
Object Transpose literal
Some special operators do not convert objects into numbers or strings, but instead convert them into literal quantities.
The exact steps of the object to the literal and the exact steps of the object to the number are called valueof before calling ToString, except that the object literal does not convert the result to a string or a number before returning, i.e. there is no bold part in the above step.
So what operators can be so SB:
- +
- ==
- !=
- <,> and all relational operators
Working with date classes
Don't worry, JS and more of a situation, is to deal with the date type.
If the "+" and "= =" encounters a date type object, it takes the object-to-string step of removing the bold part, that is, calling ToString and then calling valueof, and the result is not type-converted.
Summarize
For general conversions See table, +, = =,! =, <> and relational operators for literary conversion see Object-to-literal, +, = = 2B conversion to meet date see the process dates class.
Various types of conversion rules in JS (goto)