对于number、string、Boolean、object、symbol数据类型调用valueOf方法,得到的都是数据本身(null、undefined两种类型上的原型链上没有valueOf方法)
点击查看代码
var a = 1;
var aa = a.valueOf();
console.log(aa == a); //true
var b = 'a';
var bb = b.valueOf();
console.log(bb === b); //true
var c = true;
var cc = c.valueOf();
console.log(cc === c); //true
var obj = {
name: 'age'
};
var objobj = obj.valueOf();
console.log(obj === objobj); //true
var s = Symbol();
var ss = s.valueOf();
console.log(ss === s); //true
点击查看代码
var a = 1;
var aaa = a.toString(); // "1"
var b = 'a';
var bbb = b.toString(); // "a"
var c = true;
var ccc = c.toString(); // "true"
var obj = {
name: 'age',
};
var objobjobj = obj.toString(); //"[object Object]"
var s = Symbol();
var sss = s.toString(); //"Symbol()"
点击查看代码
var a = {
i: 0,
valueOf: function() {
console.log('valueOf')
return ++this.i;
},
}
console.log(a == 1 && a == 2 && a == 3); //true