var m = { 
    i:10, 
    toString:function () { 
        console.log('toString'); 
        return this.i; 
    }, 
    valueOf:function () { 
        console.log('valueOf'); 
        return this.i; 
    } 
}; 
console.log(m);// toString 
console.log(String(m)); // toString 
console.log(+m); //  valueOf 
console.log('' + m); // valueOf 
console.log(Number(m)); // valueOf 
console.log(m == '10'); //  valueOf 

console.log(m === '10'); // false //=== 不进行转换

在有操作符的情况 下,valueOf的优先级本来就比toString的高。valueOf解决值运算问题,toString解决显示问题。

相关文章:

  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
  • 2022-03-03
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案