# baseToNumber

# Description

转换为数字,toNumber 的基本实现

# Params

value

# Return

Number

# Depend

    import isSymbol from '../isSymbol.js'

isSymbol 源码分析

# Code

    /** Used as references for various `Number` constants. */
    const NAN = 0 / 0
    
    function baseToNumber(value) {
      if (typeof value === 'number') {
        return value
      }
      if (isSymbol(value)) {
        return NAN
      }
      return +value
    }

# Analyze

  1. valuenumber, 直接返回 value
  2. valuesymbol, 返回 NaN
  3. 否者直接返回 +value(一元正号运算符)

# Remark

  1. Number MDN (opens new window)
  2. 一元正号 MDN (opens new window)

# Example

    baseToNumber(1) // 1
    baseToNumber('3') // 3