每日一个lodash方法之add

本文详细解析lodash库中的add方法,该方法用于两数相加。通过源码分析,探讨了createMathOperation函数的角色,以及为何不直接进行数值运算。文章还深入到baseToString和baseToNumber方法,解释它们如何处理不同类型的值,特别是转换symbol和处理nullish值的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

lodash源码:

/**

* Adds two numbers.

*

* @since 3.4.0

* @category Math

* @param {number} augend The first number in an addition.

* @param {number} addend The second number in an addition.

* @returns {number} Returns the total.

* @example

*

* add(6, 4)

* // => 10

*/

const add = createMathOperation((augend, addend) => augend + addend, 0)

首先createMathOperation是什么?官方文档定义是Creates a function that performs a mathematical operation on two values.创建一个对两个值进行数学运算的方法。

源码如下:

/**

* Creates a function that performs a mathematical operation on two values.

*

* @private

* @param {Function} operator The function to perform the operation.

* @param {number} [defaultValue] The value used for `undefined` arguments.

* @returns {Function} Returns the new mathematical operation function.

*/

 

function createMathOperation(operator, defaultValue) {

return (value, other) => {

if (value === undefined && other === undefined) {

return defaultValue

}

if (value !== undefined && other === undefined) {

return value

}

if (other !== undefined && value === undefined) {

return other

}

if (typeof value === 'string' || typeof other === 'string') {

value = baseToString(value)

other = baseToString(other)

}

else {

value = baseToNumber(value)

other = baseToNumber(other)

}

return operator(value, other)

}

}

export default createMathOperation

 

为什么不直接对数值进行运算?这是为了运算结果不会出现undefined 和的情况。

源码其中出现baseToString和baseToNumber这两个方法,我们可以猜测它们的作用是为了将其他类型转化成string、number。究竟对不对?马上看源码

===============================================================

import isSymbol from '../isSymbol.js'

/** Used as references for various `Number` constants. */

const INFINITY = 1 / 0

/** Used to convert symbols to primitives and strings. */

// Symbol.prototype.toString将symbol转化成本体字符串

// 注意:

// let value = Symbol('yes');

// Symbol.prototype.toString.call(value)输出"Symbol(yes)"

const symbolToString = Symbol.prototype.toString

/**

* The base implementation of `toString` which doesn't convert nullish

* values to empty strings.

* 该toString的基本实现不能转化空值为空字符串

* @private

* @param {*} value The value to process.

* @returns {string} Returns the string.

*/

function baseToString(value) {

// Exit early for strings to avoid a performance hit in some environments.

if (typeof value == 'string') {

return value

}

if (Array.isArray(value)) {

// Recursively convert values (susceptible to call stack limits).

// 递归地转化值(容易导致内存溢出)

return `${value.map(baseToString)}`

// `${["1", "2", "3", "4"]}`会转化成"1,2,3,4"

// 从实验得出${}会将里面的对象进行toString操作

}

if (isSymbol(value)) {

return symbolToString ? symbolToString.call(value) : ''

}

const result = `${value}`

// 处理 `${-0}` = '0' 的情况

return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result

}

===============================================================

/** Used as references for various `Number` constants. */

const NAN = 0 / 0

/**

* The base implementation of `toNumber` which doesn't ensure correct

* conversions of binary, hexadecimal, or octal string values.

*

* @private

* @param {*} value The value to process.

* @returns {number} Returns the number.

*/

function baseToNumber(value) {

if (typeof value == 'number') {

return value

}

if (isSymbol(value)) {

return NAN

}

return +value // 返回value+value有什么区别

}

 

baseToNumber很简单只是处理数字和symbol,不明白返回value和+value有什么区别

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值