_.after(n, func)
{#after}
_.before
的反义词; 此方法创建一个函数,该函数在调用 n
次或更多次后调用 func
。
引入版本 0.1.0
参数
n
(number): 调用 func 之前的调用次数。func
(Function): 要限制的功能。
返回
(Function): 返回新的受限函数。
示例
const saves = ['profile', 'settings']
const done = _.after(saves.length, () => {
console.log('done saving!')
})
_.forEach(saves, (type) => {
asyncSave({ type, complete: done })
})
_.ary(func, [n=func.length])
{#ary}
创建一个调用 func
的函数,最多有 n
个参数,忽略任何其他参数。
引入版本 3.0.0
参数
func
(Function): 为其设置参数的函数。[n=func.length]
(number): 元数上限。
返回
(Function): 返回新的上限函数。
示例
_.map(['6', '8', '10'], _.ary(Number.parseInt, 1))
_.before(n, func)
{#before}
创建一个调用 func 的函数,使用创建函数的 this 绑定和参数,而它的调用次数少于 n 次。 对创建的函数的后续调用返回最后一次 func 调用的结果。
引入版本 3.0.0
参数
n
(number): 不再调用 func 的调用次数。func
(Function): 要限制的功能。
返回
(Function): 返回新的受限函数。
示例
jQuery(element).on('click', _.before(5, addContactToList))
_.bind(func, thisArg, [partials])
{#bind}
创建一个调用 func 的函数,该函数将 thisArg
的 this
绑定和 partials
附加到它接收的参数中。
_.bind.placeholder
值,在单体构建中默认为 _
,可用作部分应用参数的占位符。
注意: 与原生 Function#bind
不同,此方法不设置绑定函数的“长度”属性。
引入版本 0.1.0
参数
func
(Function): 要绑定的函数。thisArg
(*):func
的this
绑定。[partials]
(...*): 要部分应用的参数。
返回
(Function): 返回新的绑定函数。
示例
function greet(greeting, punctuation) {
return `${greeting} ${this.user}${punctuation}`
}
const object = { user: 'fred' }
const bound = _.bind(greet, object, 'hi')
bound('!')
const bound2 = _.bind(greet, object, _, '!')
bound2('hi')
_.bindKey(object, key, [partials])
{#bindKey}
创建一个函数,调用 object[key]
处的方法,并在其接收的参数前附加 partials
。
此方法与 _.bind
的不同之处在于允许绑定函数引用可能已重新定义或尚不存在的方法。 有关详细信息,请参阅 Peter Michaux 的文章。
_.bindKey.placeholder
值,在单体构建中默认为 _
,可用作部分应用参数的占位符。
引入版本 0.10.0
参数
object
(Object): 调用方法的对象。key
(string): 方法的关键。[partials]
(...*): 要部分应用的参数。
返回
(Function): 返回新的绑定函数。
示例
const object = { user: 'fred', greet(greeting, punctuation) {
return `${greeting} ${this.user}${punctuation}`
} }
const bound = _.bindKey(object, 'greet', 'hi')
bound('!')
object.greet = function (greeting, punctuation) {
return `${greeting}ya ${this.user}${punctuation}`
}
bound('!')
const bound2 = _.bindKey(object, 'greet', _, '!')
bound2('hi')
_.curry(func, [arity=func.length])
{#curry}
创建一个接受 func
参数的函数,如果至少提供了 arity
个参数,则调用 func
返回其结果,或者返回一个接受剩余 func
参数的函数,依此类推。 如果 func.length 不够,可以指定 func 的数量。
_.curry.placeholder
值,在单体构建中默认为 _
,可用作提供参数的占位符。
注意: 此方法不设置柯里化函数的“长度”属性。
引入版本 2.0.0
参数
func
(Function): 咖喱的功能。[arity=func.length]
(number):func
的参数。
返回
(Function): 返回新的柯里化函数。
示例
const abc = function (a, b, c) {
return [a, b, c]
}
const curried = _.curry(abc)
curried(1)(2)(3)
curried(1, 2)(3)
curried(1, 2, 3)
curried(1)(_, 3)(2)
_.curryRight(func, [arity=func.length])
{#curryRight}
此方法类似于 _.curry
,只是参数以 _.partialRight
而不是 _.partial
。
_.curryRight.placeholder
值,在单体构建中默认为 _
,可以用作提供参数的占位符。
注意: 此方法不设置柯里化函数的“长度”属性。
引入版本 3.0.0
参数
func
(Function): 咖喱的功能。[arity=func.length]
(number):func
的参数。
返回
(Function): 返回新的柯里化函数。
示例
const abc = function (a, b, c) {
return [a, b, c]
}
const curried = _.curryRight(abc)
curried(3)(2)(1)
curried(2, 3)(1)
curried(1, 2, 3)
curried(3)(1, _)(2)
_.debounce(func, [wait=0], [options={}])
{#debounce}
创建一个去抖动函数,该函数延迟调用 func 直到自上次调用去抖动函数后经过 wait
毫秒。 debounced 函数带有一个 cancel
方法来取消延迟的 func
调用和一个 flush
方法来立即调用它们。提供 options
来指示是否应该在 wait
超时的前沿和/或后沿调用 func
。 func
是使用提供给去抖动函数的最后一个参数调用的。对去抖动函数的后续调用返回最后一次 func 调用的结果。
注意: 如果 leading
和 trailing
选项为 true
,则只有在 wait
超时期间多次调用去抖动函数时,才会在超时后沿调用 func
。
如果 wait
为 0
且 leading
为 false
,则 func
调用被推迟到下一个滴答声,类似于 setTimeout
的超时时间为 0
。
有关 _.debounce
和 _.throttle
。
引入版本 0.1.0
参数
func
(Function): 去抖动的功能。[wait=0]
(number): 要延迟的毫秒数。[options={}]
(Object): 选项对象。[options.leading=false]
(boolean): 指定在超时的前沿调用。[options.maxWait]
(number):func
在被调用之前被允许延迟的最长时间。[options.trailing=true]
(boolean): 指定在超时后沿调用。
返回
(Function): 返回新的去抖动函数。
示例
jQuery(window).on('resize', _.debounce(calculateLayout, 150))
jQuery(element).on('click', _.debounce(sendMail, 300, { leading: true, trailing: false }))
const debounced = _.debounce(batchLog, 250, { maxWait: 1000 })
const source = new EventSource('/stream')
jQuery(source).on('message', debounced)
jQuery(window).on('popstate', debounced.cancel)
_.defer(func, [args])
{#defer}
推迟调用 func 直到当前调用堆栈被清除。调用时会向 func 提供任何附加参数。
引入版本 0.1.0
参数
func
(Function): 要延迟的功能。[args]
(...*): 调用 func 的参数。
返回
(number): 返回计时器 ID。
示例
_.defer((text) => {
console.log(text)
}, 'deferred')
_.delay(func, wait, [args])
{#delay}
在 wait
毫秒后调用 func
。调用时会向 func 提供任何附加参数。
引入版本 0.1.0
参数
func
(Function): 延迟的功能。wait
(number): 延迟调用的毫秒数。[args]
(...*): 调用 func 的参数。
返回
(number): 返回计时器 ID。
示例
_.delay((text) => {
console.log(text)
}, 1000, 'later')
_.flip(func)
{#flip}
创建一个调用 func
的函数,参数颠倒。
引入版本 4.0.0
参数
func
(Function): 翻转参数的函数。
返回
(Function): 返回新的翻转函数。
示例
const flipped = _.flip(function () {
// eslint-disable-next-line prefer-rest-params
return _.toArray(arguments)
})
flipped('a', 'b', 'c', 'd')
_.memoize(func, [resolver])
{#memoize}
创建一个函数来记忆 func 的结果。如果提供了 resolver
,它会根据提供给 memoized 函数的参数确定用于存储结果的缓存键。默认情况下,提供给 memoized 函数的第一个参数用作地图缓存键。 func
是通过 memoized 函数的 this
绑定调用的。
注意: 缓存在 memoized 函数中作为 cache
属性公开。它的创建可以通过将 _.memoize.Cache
构造函数替换为实例实现 [Map
](http://ecma-international.org/ecma-262/7.0/#sec-properties-of -the-map-prototype-object) clear
、delete
、get
、has
和 set
的方法接口。
引入版本 0.1.0
参数
func
(Function): 将其输出记忆化的函数。[resolver]
(Function): 解析缓存键的函数。
返回
(Function): 返回新的记忆函数。
示例
const object = { a: 1, b: 2 }
const other = { c: 3, d: 4 }
const values = _.memoize(_.values)
values(object)
values(other)
object.a = 2
values(object)
values.cache.set(object, ['a', 'b'])
values(object)
_.memoize.Cache = WeakMap
_.negate(predicate)
{#negate}
创建一个否定谓词 func 的结果的函数。 func
谓词使用this
绑定和创建函数的参数调用。
引入版本 3.0.0
参数
predicate
(Function): 否定的谓词。
返回
(Function): 返回新的否定函数。
示例
function isEven(n) {
return n % 2 === 0
}
_.filter([1, 2, 3, 4, 5, 6], _.negate(isEven))
_.once(func)
{#once}
创建一个仅限于调用 func
一次的函数。重复调用函数返回第一次调用的值。 func
是通过 this
绑定和创建函数的参数调用的。
引入版本 0.1.0
参数
func
(Function): 要限制的功能。
返回
(Function): 返回新的受限函数。
示例
const initialize = _.once(createApplication)
initialize()
initialize()
_.overArgs(func, [transforms=[_.identity]])
{#overArgs}
创建一个调用 func
并转换其参数的函数。
引入版本 4.0.0
参数
func
(Function): 要包装的函数。[transforms=[_.identity]]
(...(Function|Function[])): 论据转变。
返回
(Function): 返回新函数。
示例
function doubled(n) {
return n * 2
}
function square(n) {
return n * n
}
const func = _.overArgs((x, y) => {
return [x, y]
}, [square, doubled])
func(9, 3)
func(10, 5)
_.partial(func, [partials])
{#partial}
创建一个调用 func 的函数,在它接收的参数前附加 partials
。 此方法类似于 _.bind
,但它不 更改 this
绑定。
_.partial.placeholder
值,在整体构建中默认为 _
,可用作部分应用参数的占位符。
注意: 此方法不设置部分应用函数的“长度”属性。
引入版本 0.2.0
参数
func
(Function): 部分应用参数的函数。[partials]
(...*): 要部分应用的参数。
返回
(Function): 返回新的部分应用函数。
示例
function greet(greeting, name) {
return `${greeting} ${name}`
}
const sayHelloTo = _.partial(greet, 'hello')
sayHelloTo('fred')
const greetFred = _.partial(greet, _, 'fred')
greetFred('hi')
_.partialRight(func, [partials])
{#partialRight}
此方法与 _.partial
类似,只是将部分应用的参数附加到它接收的参数中。
_.partialRight.placeholder
值,在单体构建中默认为 _
,可用作部分应用参数的占位符。
注意: 此方法不设置部分应用函数的“长度”属性。
引入版本 1.0.0
参数
func
(Function): 部分应用参数的函数。[partials]
(...*): 要部分应用的参数。
返回
(Function): 返回新的部分应用函数。
示例
function greet(greeting, name) {
return `${greeting} ${name}`
}
const greetFred = _.partialRight(greet, 'fred')
greetFred('hi')
const sayHelloTo = _.partialRight(greet, 'hello', _)
sayHelloTo('fred')
_.rearg(func, indexes)
{#rearg}
创建一个调用 func 的函数,其参数根据指定的索引排列,其中第一个索引处的参数值作为第一个参数提供,第二个索引处的参数值作为第二个参数提供,依此类推 .
引入版本 3.0.0
参数
func
(Function): 重新排列参数的函数。indexes
(...(number|number[])): 排列的参数索引。
返回
(Function): 返回新函数。
示例
const rearged = _.rearg((a, b, c) => {
return [a, b, c]
}, [2, 0, 1])
rearged('b', 'c', 'a')
_.rest(func, [start=func.length-1])
{#rest}
创建一个函数,该函数使用已创建函数的 this
绑定和来自 start
的参数以及作为数组提供的参数调用 func
。
**注意:**此方法基于rest参数。
引入版本 4.0.0
参数
func
(Function): 应用休息参数的函数。[start=func.length-1]
(number): rest 参数的起始位置。
返回
(Function): 返回新函数。
示例
const say = _.rest((what, names) => {
return `${what} ${_.initial(names).join(', ')}${_.size(names) > 1 ? ', & ' : ''}${_.last(names)}`
})
say('hello', 'fred', 'barney', 'pebbles')
_.spread(func, [start=0])
{#spread}
创建一个使用 create 函数的 this
绑定和一个参数数组调用 func
的函数,类似于 Function#apply
。
**注意:**此方法基于spread operator。
引入版本 3.2.0
参数
func
(Function): 传播参数的函数。[start=0]
(number): 点差的起始位置。
返回
(Function): 返回新函数。
示例
const say = _.spread((who, what) => {
return `${who} says ${what}`
})
say(['fred', 'hello'])
const numbers = Promise.all([Promise.resolve(40), Promise.resolve(36)])
numbers.then(_.spread((x, y) => {
return x + y
}))
_.throttle(func, [wait=0], [options={}])
{#throttle}
创建一个节流函数,每 wait
毫秒最多只调用一次 func
。节流函数带有一个 cancel
方法来取消延迟的 func
调用和一个 flush
方法来立即调用它们。提供 options
来指示是否应该在 wait
超时的前沿和/或后沿调用 func
。 func
是使用提供给限制函数的最后一个参数调用的。对节流函数的后续调用返回最后一次 func 调用的结果。
注意: 如果 leading
和 trailing
选项为 true
,则只有在 wait
超时期间多次调用节流函数时,才会在超时后沿调用 func
。
如果 wait
为 0
且 leading
为 false
,则 func
调用被推迟到下一个滴答声,类似于 setTimeout
的超时时间为 0
。
有关 _.throttle
和 _.debounce
。
引入版本 0.1.0
参数
func
(Function): 节流的功能。[wait=0]
(number): 限制调用的毫秒数。[options={}]
(Object): 选项对象。[options.leading=true]
(boolean): 指定在超时的前沿调用。[options.trailing=true]
(boolean): 指定在超时后沿调用。
返回
(Function): 返回新的节流函数。
示例
jQuery(window).on('scroll', _.throttle(updatePosition, 100))
const throttled = _.throttle(renewToken, 300000, { trailing: false })
jQuery(element).on('click', throttled)
jQuery(window).on('popstate', throttled.cancel)
_.unary(func)
{#unary}
创建一个最多接受一个参数的函数,忽略任何其他参数。
引入版本 4.0.0
参数
func
(Function): 为其设置参数的函数。
返回
(Function): 返回新的上限函数。
示例
_.map(['6', '8', '10'], _.unary(Number.parseInt))
_.wrap(value, [wrapper=identity])
{#wrap}
创建一个为 wrapper
提供 value
作为其第一个参数的函数。 提供给函数的任何附加参数都附加到提供给 wrapper
的参数。 包装器是通过创建函数的 this
绑定调用的。
引入版本 0.1.0
参数
value
(*): 要包装的值。[wrapper=identity]
(Function): 包装函数。
返回
(Function): 返回新函数。
示例
const p = _.wrap(_.escape, (func, text) => {
return `<p>${func(text)}</p>`
})
p('fred, barney, & pebbles')