Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bdb4b71

Browse files
feat: add js solution to lc problem: No.2666 (doocs#3340)
1 parent 32ac4a4 commit bdb4b71

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
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 -->

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ onceFn(4, 6, 8); // undefined, fn was not called
6868
#### TypeScript
6969

7070
```ts
71-
function once<T extends (...args: any[]) => any>(
72-
fn: T,
73-
): (...args: Parameters<T>) => ReturnType<T> | undefined {
71+
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
72+
type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
73+
74+
function once(fn: Function): OnceFn {
7475
let called = false;
7576
return function (...args) {
7677
if (!called) {
@@ -89,6 +90,32 @@ function once<T extends (...args: any[]) => any>(
8990
*/
9091
```
9192

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

94121
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {Function} fn
3+
* @return {Function}
4+
*/
5+
var once = function (fn) {
6+
let called = false;
7+
return function (...args) {
8+
if (!called) {
9+
called = true;
10+
return fn(...args);
11+
}
12+
};
13+
};
14+
15+
/**
16+
* let fn = (a,b,c) => (a + b + c)
17+
* let onceFn = once(fn)
18+
*
19+
* onceFn(1,2,3); // 6
20+
* onceFn(2,3,6); // returns undefined without calling fn
21+
*/

‎solution/2600-2699/2666.Allow One Function Call/Solution.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
function once<T extends (...args: any[]) => any>(
2-
fn: T,
3-
): (...args: Parameters<T>) => ReturnType<T> | undefined {
1+
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
2+
type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
3+
4+
function once(fn: Function): OnceFn {
45
let called = false;
56
return function (...args) {
67
if (!called) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /