Skip to content

Commit d7c81cc

Browse files
author
phillzou
committed
feat: add before function
1 parent 5393526 commit d7c81cc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- [函数节流](./src/throttle.js) ★★★
3434
- [函数柯里化(实现 `sum(1,2)(3)()`](./src/curry.js) ★★★
3535
- [compose 函数](./src/compose.js) ★★
36+
- [before 函数](./src/before.js)
3637

3738

3839
## 数组

src/before.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 传入任意一个函数,只能调用指定的次数
3+
* @param {*} count 调用次数
4+
* @param {*} func 传入函数
5+
* @returns
6+
*/
7+
function before(count, func) {
8+
var temp = count;
9+
return function() {
10+
if (temp > 1) {
11+
temp--;
12+
const args = [...arguments];
13+
func.apply(this, args);
14+
}
15+
}
16+
}
17+
18+
const log = a => console.log(a);
19+
20+
const log3 = before(3, log);
21+
22+
log3(2);
23+
log3(1);
24+
log3(3);

0 commit comments

Comments
 (0)