_call

说明

模拟实现call方法

源码

用法

_call.test.ts
1
import { _call } from "../../utility/function/_call";
2
3
export type FunctionKeyType = keyof typeof Function;
4
5
describe('_call', () => {
6
test('_call should change the `this` when processing', () => {
7
const origin = {
8
name: 'ddzy',
9
};
10
const expected = {
11
name: 'ddzy',
12
arg0: 0,
13
arg1: 1,
14
};
15
16
const received = {
17
printName(...args: any[]) {
18
const name = this['name' as keyof typeof received];
19
const arg0 = args[0];
20
const arg1 = args[1];
21
22
expect({ name, arg0, arg1 }).toEqual(expected);
23
},
24
};
25
26
Function.prototype['_call' as FunctionKeyType] = _call;
27
28
received.printName['_call' as FunctionKeyType](origin, 0, 1);
29
});
30
});
Copied!
最近更新 1yr ago
复制链接