_bind

说明

模拟实现bind方法

源码

用法

_bind.test.ts
1
import { _bind } from "../../utility/function/_bind";
2
3
export type FunctionKeyType = keyof typeof Function;
4
5
describe('_bind', () => {
6
test('_bind should return `function` which can change the `this`', () => {
7
const origin = {
8
name: 'ddzy is a fe programmer',
9
};
10
const expected = {
11
name: 'ddzy is a fe programmer',
12
arg0: 20,
13
arg1: 30,
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['_bind' as FunctionKeyType] = _bind;
27
28
const _bindedFunc = received.printName['_bind' as FunctionKeyType](origin);
29
_bindedFunc([20, 30]);
30
})
31
})
Copied!
最近更新 1yr ago
复制链接