_.flip(func)

104

_.flip(func)

_.flip创建一个函数调用func函数并且将接收到的参数反转顺序后传入

参数

func (Function): 需要反转参数顺序处理的函数

返回值

(Function): 返回新的反转传入参数的函数

例子

var flipped = _.flip(function() {
  return _.toArray(arguments);
});
 
flipped('a', 'b', 'c', 'd');
// => ['d', 'c', 'b', 'a']

源代码:

/**
 * Creates a function that invokes `func` with arguments reversed.
 *
 * @since 4.0.0
 * @category Function
 * @param {Function} func The function to flip arguments for.
 * @returns {Function} Returns the new flipped function.
 * @see reverse
 * @example
 *
 * const flipped = flip((...args) => args)
 *
 * flipped('a', 'b', 'c', 'd')
 * // => ['d', 'c', 'b', 'a']
 */
//创建一个函数调用func函数并且将接收到的参数反转顺序后传入
function flip(func) {
  if (typeof func != 'function') {//如果func不是函数,抛出错误
    throw new TypeError('Expected a function')
  }
  return function(...args) {//返回新建的函数,apply调用func,参数列表被反转顺序
    return func.apply(this, args.reverse())
  }
}

export default flip

 

posted @ 2018-11-27 15:08  hahazexia  阅读(184)  评论(0)    收藏  举报