美文网首页
装箱拆箱

装箱拆箱

作者: maomizone | 来源:发表于2022-04-18 21:13 被阅读0次
      let num = 10
      // num是原始值,不是对象,按常理来讲,是不能做'成员访问'
      // 默认装箱操作:new Number([val]) 变为非标准特殊对象,这样就可以调用Number.prototype上的方法了
      console.log(num.toFixed(2)) // 10.00

      let number = new Number(20)
      // 对象转数字,把new Number(20)这个非标准特殊对象变为原始值的操作为拆箱
      console.log(num + number) // 30

      console.log(number[Symbol.toPrimitive]) // undefined
      console.log(number.valueOf()) // 20

相关文章

网友评论

      本文标题:装箱拆箱

      本文链接:https://www.haomeiwen.com/subject/vfllertx.html