_.add(augend, addend)
173
_.add(augend, addend)
_.add将两个数字相加返回和
参数
augend (number): 相加的第一个数字
addend (number): 相加的第二个数字
返回值
(number): 返回和
例子
_.add(6, 4); // => 10
源代码
import createMathOperation from './.internal/createMathOperation.js' /** * 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方法用于创建一个对两个值做数学操作的方法,第一个参数是对两个值所做的操作,第二个参数是对undefined值所使用的替换默认值 export default add
createMathOperation
import baseToNumber from './baseToNumber.js' import baseToString from './baseToString.js' /** * 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. */ //createMathOperation方法用于创建一个对两个值做数学操作的方法,第一个参数是对两个值所做的操作,第二个参数是对undefined值所使用的替换默认值 function createMathOperation(operator, defaultValue) { return (value, other) => {//返回这个数学操作方法 if (value === undefined && other === undefined) { //如果两个操作值都是undefined,返回默认值 return defaultValue } if (value !== undefined && other === undefined) { //如果value不是undefined而other是undefined,返回value return value } if (other !== undefined && value === undefined) { //如果other不是undefined而value是undefined,返回other 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
baseToNumber
import isSymbol from '../isSymbol.js' /** 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. */ //toNumber的基础实现,不保证对二进制,十六进制,八进制字符串值的转换是否是正确的 function baseToNumber(value) { if (typeof value == 'number') {//如果value是数字,直接返回 return value } if (isSymbol(value)) {//如果value是symbol对象,返回NaN return NAN } return +value//使用+隐式转换value为数字 } export default baseToNumber
baseToString
import isSymbol from '../isSymbol.js' /** Used as references for various `Number` constants. */ const INFINITY = 1 / 0 /** Used to convert symbols to primitives and strings. */ const symbolProto = Symbol ? Symbol.prototype : undefined const symbolToString = symbolProto ? symbolProto.toString : undefined /** * The base implementation of `toString` which doesn't convert nullish * values to empty strings. * * @private * @param {*} value The value to process. * @returns {string} Returns the string. */ //toString的基础实现,不会将null值转换成空字符串 function baseToString(value) { // Exit early for strings to avoid a performance hit in some environments. if (typeof value == 'string') {//如果value本来就是字符串,直接返回 return value } if (Array.isArray(value)) {//如果是数组,循环数组每个元素递归调用baseToString转换每一个数组元素变成字符串 // Recursively convert values (susceptible to call stack limits). return `${value.map(baseToString)}` } if (isSymbol(value)) {//如果value是symbol对象,调用Symbol.prototype.toString转换 return symbolToString ? symbolToString.call(value) : '' } const result = `${value}` return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result //处理value原本是负数的情况 } export default baseToString