数据类型相互转换-转为字符串

2022-04-17  本文已影响0人  maomizone

转换规则

@1 拿字符串包起来
@2 特殊:Object.prototype.toString

出现情况

@1 String([val]) 遇到对象有三个步骤

@2 [val].toString():arr.toString() -> ''(空串)    obj.toString()->'[object Object]',但是
Object.prototype.toString.call([])->'[object Array]'
@3 '+'除数学运算,还可能代表字符串拼接

      // 100 + 1 + 21.2 + 0 + NaN = NaN 之后进行字符串拼接
      // 'NaN' + 'Tencent' + '' + 'null' + '9' + 'false' + '[object Object]'
      let res = 100 + true + 21.2 + null + undefined + 'Tencent' + [] + null + 9 + false + {}
      console.log(res); // 'NaNTencentnull9false[object Object]'
     let num = '-1'
     num = +num
     console.log(typeof num, num); // 'number' -1
上一篇 下一篇

猜你喜欢

热点阅读