Skip to content

Commit 9b4f00b

Browse files
authored
Update README.md
1 parent 738cbe8 commit 9b4f00b

File tree

1 file changed

+30
-3
lines changed
  • solution/2600-2699/2666.Allow One Function Call

1 file changed

+30
-3
lines changed

solution/2600-2699/2666.Allow One Function Call/README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ onceFn(4, 6, 8); // undefined, fn 没有被调用
7070
#### TypeScript
7171

7272
```ts
73-
function once<T extends (...args: any[]) => any>(
74-
fn: T,
75-
): (...args: Parameters<T>) => ReturnType<T> | undefined {
73+
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
74+
type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
75+
76+
function once(fn: Function): OnceFn {
7677
let called = false;
7778
return function (...args) {
7879
if (!called) {
@@ -91,6 +92,32 @@ function once<T extends (...args: any[]) => any>(
9192
*/
9293
```
9394

95+
#### JavaScript
96+
97+
```js
98+
/**
99+
* @param {Function} fn
100+
* @return {Function}
101+
*/
102+
var once = function (fn) {
103+
let called = false;
104+
return function (...args) {
105+
if (!called) {
106+
called = true;
107+
return fn(...args);
108+
}
109+
};
110+
};
111+
112+
/**
113+
* let fn = (a,b,c) => (a + b + c)
114+
* let onceFn = once(fn)
115+
*
116+
* onceFn(1,2,3); // 6
117+
* onceFn(2,3,6); // returns undefined without calling fn
118+
*/
119+
```
120+
94121
<!-- tabs:end -->
95122

96123
<!-- solution:end -->

0 commit comments

Comments
 (0)