Type Conversion
JavaScript needs to be converted based on the type of their own
value | String | digital | Boolean value | Objects |
---|---|---|---|---|
undefined | "undefined" | NaN | false | throw TypeError |
null | "null" | 0 | false | throw TypeError |
true | "true" | 1 | new Boolean(true) | |
false | "false" | 0 | new Boolean(false) | |
"" (The empty string) | 0 | false | new String("") | |
"2.3" | 2.3 | true | new String("2.3") | |
"test" | NaN | true | new String("test") | |
0 | "0" | false | new Number(0) | |
-0 | "0" | false | new Number(-0) | |
NaN | "NaN" | false | new Number(NaN) | |
Infinity | "Infinity" | true | new Number(Infinity) | |
-Infinity | "-Infinity" | true | new Number(-Infinity) | |
1 | "1" | true | new Number(1) | |
{} | See below the object into the original value | See below the object into the original value | true | |
[] | "" | 0 | true | |
[1] | "1" | 1 | true | |
['a'] | Use join () method | NaN | true | |
function(){} | NaN | true |
- In addition
null
, andundefined
any value other than bothtoString()
methods, the results of this method and generallyString()
consistent with the method - All objects (including arrays and functions) for
bool
the conversion are converted totrue
, for example:!!new Boolean(false)
, the result istrue
Object into the original value
A method!> And object to a String object to a digital conversion to be converted is done by calling the object. Javascript object has two different methods to perform the conversion.
All objects inherit two conversion methods.
- The first is
toString()
that it's a reflection of the role is to return this object string.
JavaScript in the local object often defines its owntoString()
methods, such as:
class | toString() Method to realize |
Examples |
---|---|---|
Ordinary objects | Return Type | ({a:1}).toString() ==> "[object Object]" |
Array type (Array class) | Converting each array element as a string, and a , connector |
[1,2,3].toString() ==> "1,2,3" |
Class of functions (Function class) | Return of this function implementation-defined representation, i.e. a user-defined function converts JavaScript source code string | (function() {console.log(1)}).toString() ==> "function() {console.log(1)}" |
Date class (Date class) | Returns a readable (may be parsed Javascript) date and time strings | (new Date(2019, 5, 31)).toString ==> "Mon Jul 01 2019 00:00:00 GMT+0800 (中国标准时间)" |
RegExp类(RegExp class) | Returns a regular expression amount of direct string | /\d+/g.toString() ==> "/\d+/g" |
- Another function converts the object is
valueOf()
. The task of this method is not defined in detail: If any of the original value exists, it will default object into a representation of its original value. The object is a composite value, and most objects can not be represented as a true original value, so the defaultvalueOf()
method simply returns the object itself, rather than a return to the original value.
Arrays, functions and regular expressions simply inherited this default method, calling Examples of these types of valueOf()
method simply returns the object itself.
Date class defines the valueOf()
method will return its internal representation: the number of milliseconds since January 1, 1970
(new Date(2019, 5, 31)).valueOf() // => 1561910400000
JavaScript objects to string conversion
- If the object has a
toString()
method, this method is invoked. If it returns a primitive value, JavaScript this value to a string (if not itself a string), and returns the result string. - If the object does not
toString()
approach or this method does not return a primitive value, then the Javascript calls thevalueOf()
method. If this method exists, the JavaScript call it. If the return value is the original value, JavaScript will convert the value to a string (if itself is not a string) and returns the string result. - Otherwise, JavaScript can not be from
toString()
orvalueOf()
get a raw value, then he will throw a type error exception
JavaScript objects to digital conversion process
Objects to digital conversion process with the object to a string of similar conversion process, but he will first try to use the valueOf()
method:
- If the object has
valueOf()
a method, which returns an original value if, JavaScript then these converted into digital raw value (if necessary), and returns this number - Otherwise, if the object has
toString()
a method, which returns if a raw value, JavaScript and convert it to a digital return - Otherwise, throw an error exception type
Examples
Example: empty array into a digital 0
+[] // => 0
Example: the array contains a number of elements into a digital, the digital result is
+[100] // => 100
Explanation: Array inherits default
valueOf()
method, which returns an object rather than an original value, and therefore continue to call thetoString()
method; an empty string is converted to an empty array, then an empty string is converted to a digital 0. Similarly, comprising100
array into a string"100"
,
then converted to digital100
Example:
[] + [] // => '',在这个过程中先调用valueOf()方法,返回的数组的原始值还是一个数组,再调用toString()方法,即为:"" + "" =""
[1,2] + 1 // => '1,21', [1,2] 先调用valueOf(),返回的还是数组本身,在调用toString(),返回字符串"1,2",则 "1,2" + 1 ="1,21"
[1] + 1 // => 2
1 + {a:1} // => "1[object Object]"
{a:1} +1 // => 1, 这是因为js在解释代码时遇到{}会认为是一个代码块(代码区域),{a:1}已经结束的代码块,因此相当于原式= +1 // 1
new Number(1) == 1 // => true
Two of the original operator
+
, if one of the operands is an object, a method using special JavaScript (target date to try to calltoString()
, and then tries to callvalueOf()
,
only to get the original value is used directly, without further converted into a number or string ; addition to the date the object, the first call attemptvalueOf()
, then the calltoString()
, the same will not be further converted into numeric or string)
which is converted to the original value calculation;==
,!=
and relational operators Similarly, if an object with a comparison of the original value, the conversion to the original value conversion method performed in accordance with the object;
example:
let now = new Date();
now + 1 // "Fri May 31 2019 17:10:20 GMT+0800 (中国标准时间)1"
now - 1 // 1562034278023
now == now.toString() // true
now > (now - 1) // true
(!+[]+[]+![]).length // 9
(!(~+[]) + {})[--[~+""][+[]] * [~+[]] + ~~!+[]] + ({} + [])[[~!+[]] * ~+[]] // => sb
let a = {};
a.toString = () => 2
a.valueOf = () => 3
1 + a // 4
'1' + a // '13'