每⽇⼀个lodash⽅法之add
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. */
// String将symbol转化成本体字符串
// 注意:
// let value = Symbol('yes');
// String.call(value)输出"Symbol(yes)"
const symbolToString = String
/**
* 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 correctlodash有哪些方法
* 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有什么区别
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论