lodash analysis
  • Introduction
  • Tips
    • 高阶函数
    • 从 ECMAScript 中理解 Symbol 类型转换
  • Array
    • findLastIndex
    • map
  • Collection
    • countBy
    • findLast
    • flatMap
    • partition
    • reduce
  • Function
    • after
    • before
    • debounce
    • defer
    • delay
    • flip
    • memoize
    • negate
    • once
    • overArgs
    • throttle
  • Lang
    • isArguments
    • isArrayLike
    • isBuffer
    • isLength
    • isObject
    • isObjectLike
    • isSymbol
    • isTypedArray
    • isUndefined
    • toFinite
    • toNumber
  • Math
    • add
    • ceil
    • divide
    • floor
    • maxBy
    • mean
    • meanBy
    • minBy
    • multiply
    • round
    • subtract
    • sum
    • sumBy
  • Number
    • clamp
    • inRange
    • random
  • Object
    • keys
  • .internal
    • arrayLikeKeys
    • arrayReduce
    • baseAssignValue
    • baseEach
    • baseFindIndex
    • baseFlatten
    • baseFor
    • baseForOwn
    • baseInRange
    • baseReduce
    • baseSum
    • baseToNumber
    • baseToString
    • createMathOperation
    • createRound
    • getTag
    • isFlattenable
    • isIndex
    • nodeTypes
  • Date
    • now(已弃用)
Powered by GitBook
On this page
  1. .internal

baseToNumber

PreviousbaseSumNextbaseToString

Last updated 6 years ago

Was this helpful?

CtrlK
  • 依赖
  • 源码
  • 原理
  • 相关链接

Was this helpful?

Overthinking ruins you. Ruins the situation, twists it around, makes you worry and just makes everything much worse than it actually is. — Unknown

本文为 《lodash 源码阅读》 系列文章,后续内容会在 github 中发布,欢迎 star,gitbook 同步更新。

依赖

import isSymbol from '../isSymbol.js';
  • lodash 源码阅读 —— isSymbol

源码

/** 声明 NAN 变量 */
const NAN = 0 / 0;

/**
 * `toNumber`的基本实现,它不能确保正确转换二进制,十六进制或八进制字符串值。
 *
 * @private
 * @param {*} value 需要处理的值
 * @returns 

原理

baseToNumber 相较于 Number(),主要增加了对 Symbol 转换的优化,针对 Symbol 的特殊处理原因,我另起一篇文章讲解 从 ECMAScript 中理解 Symbol 类型转换。

相关链接

  • 从 ECMAScript 中理解 Symbol 类型转换

  • lodash 源码阅读 —— isSymbol

{number}
返回一个数值
*/
function baseToNumber(value) {
if (typeof value == 'number') {
return value;
}
if (isSymbol(value)) {
return NAN;
}
return +value;
}