1 Star 0 Fork 0

UnPourTous/lodash

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
before.js 921 Bytes
一键复制 编辑 原始数据 按行查看 历史
John-David Dalton 提交于 2017-03-14 11:49 +08:00 . Remove coercion method use.
/**
* Creates a function that invokes `func`, with the `this` binding and arguments
* of the created function, while it's called less than `n` times. Subsequent
* calls to the created function return the result of the last `func` invocation.
*
* @since 3.0.0
* @category Function
* @param {number} n The number of calls at which `func` is no longer invoked.
* @param {Function} func The function to restrict.
* @returns {Function} Returns the new restricted function.
* @example
*
* jQuery(element).on('click', before(5, addContactToList))
* // => Allows adding up to 4 contacts to the list.
*/
function before(n, func) {
let result
if (typeof func != 'function') {
throw new TypeError('Expected a function')
}
return function(...args) {
if (--n > 0) {
result = func.apply(this, args)
}
if (n <= 1) {
func = undefined
}
return result
}
}
export default before
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/UnPourTous/lodash.git
git@gitee.com:UnPourTous/lodash.git
UnPourTous
lodash
lodash
master

搜索帮助