_.multiply(multiplier, multiplicand)

183

_.multiply(multiplier, multiplicand)

_.multiply将两个数字相乘

参数

multiplier (number): 相乘的第一个数字
multiplicand (number): 相乘的第二个数字

返回值

(number): 返回相乘的结果

例子

_.multiply(6, 4);
// => 24

源代码

import createMathOperation from './.internal/createMathOperation.js'

/**
 * Multiply two numbers.
 *
 * @since 4.7.0
 * @category Math
 * @param {number} multiplier The first number in a multiplication.
 * @param {number} multiplicand The second number in a multiplication.
 * @returns {number} Returns the product.
 * @example
 *
 * multiply(6, 4)
 * // => 24
 */
//将两个数字相乘
const multiply = createMathOperation((multiplier, multiplicand) => multiplier * multiplicand, 1)

export default multiply
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

 

posted @ 2019-04-24 09:52  hahazexia  阅读(1149)  评论(0)    收藏  举报