Skip to content

Commit 4924a83

Browse files
committed
feat: add ts solution to lc problem: No.2693
No.2693.Call Function with Custom Context
1 parent 433384a commit 4924a83

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

solution/2600-2699/2693.Call Function with Custom Context/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,21 @@ args = [{"item": "burger"}, 10, 1,1]
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```ts
73+
declare global {
74+
interface Function {
75+
callPolyfill(context: Record<any, any>, ...args: any[]): any;
76+
}
77+
}
78+
79+
Function.prototype.callPolyfill = function (context, ...args): any {
80+
const fn = this.bind(context);
81+
return fn(...args);
82+
};
7383

84+
/**
85+
* function increment() { this.count++; return this.count; }
86+
* increment.callPolyfill({count: 1}); // 2
87+
*/
7488
```
7589

7690
<!-- tabs:end -->

solution/2600-2699/2693.Call Function with Custom Context/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,21 @@ args = [{&quot;item&quot;: &quot;burger&quot;}, 10, 1,1]
6464
### **TypeScript**
6565

6666
```ts
67+
declare global {
68+
interface Function {
69+
callPolyfill(context: Record<any, any>, ...args: any[]): any;
70+
}
71+
}
72+
73+
Function.prototype.callPolyfill = function (context, ...args): any {
74+
const fn = this.bind(context);
75+
return fn(...args);
76+
};
6777

78+
/**
79+
* function increment() { this.count++; return this.count; }
80+
* increment.callPolyfill({count: 1}); // 2
81+
*/
6882
```
6983

7084
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare global {
2+
interface Function {
3+
callPolyfill(context: Record<any, any>, ...args: any[]): any;
4+
}
5+
}
6+
7+
Function.prototype.callPolyfill = function (context, ...args): any {
8+
const fn = this.bind(context);
9+
return fn(...args);
10+
};
11+
12+
/**
13+
* function increment() { this.count++; return this.count; }
14+
* increment.callPolyfill({count: 1}); // 2
15+
*/

0 commit comments

Comments
 (0)