本篇仅对隐式转换中的知识点做记录,不深入展开介绍.

upload successful

点击查看图片

注意: PreferredType默认为Number,当input为Date类型数据时,PreferredType为String


  1. valueOf()
var num = new Number('123');
num.valueOf(); // 123

var str = new String('12df');
str.valueOf(); // '12df'

var bool = new Boolean('fd');
bool.valueOf(); // true

var a = new Date();
a.valueOf(); // 1532234132475

var a = new Array();
a.valueOf(); //[]
a.valueOf() === a; // true

var b = new Object({});
b.valueOf(); //{}
b.valueOf() === b; // true

2.toString()

var num = new Number('123sd');
num.toString(); // 'NaN'

var str = new String('12df');
str.toString(); // '12df'

var bool = new Boolean('fd');
bool.toString(); // 'true'

var arr = new Array(1,2);
arr.toString(); // '1,2'

var d = new Date();
d.toString(); // "Sun Jul 22 2018 12:38:42 GMT+0800 (中国标准时间)"

var func = function () {}
func.toString(); // "function () {}"

var obj = new Object({});
obj.toString(); // "[object Object]"

Math.toString(); // "[object Math]"

type(x) type(y) result(x==y)
null undefined true
String Number Number(x)==y
Boolean any Number(x)==y
Object String or Number toPrimitive(x)==y
NaN any false
[]==0; //true

1.[]=>ValueOf([])=>[];
2.[]=>toSring([])=>""

""==0

3.""=>Number("")=>0

0==0
[]=="0" //false

1.[]=>ValueOf([])=>[];
2.[]=>toSring([])=>""

""=="0"

type(x) type(y) result(x+y)
String any x+toString(y)
Object any toString(x)+toString(y)
Boolean Number or Boolean Number(x)+Number(y)
"1ac"+true   // "1actrue"
"1ac"+[] // "1ac"
"1ac"+[1,'a'] // "1ac1,a"
"1ac"+{} // "1ac[object Object]"

{}+true // "[object Object]true"
{}+[] // "[object Object]"

true+true // 2
true+1 // 2
false+1 // 1