-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcall.test.js
56 lines (46 loc) · 1.36 KB
/
call.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import call from './call';
describe('Function.prototype.call', () => {
Function.prototype._call = call;
it('change the direction of this', () => {
const foo = {
value: 1,
};
function bar() {
return this.value;
}
// 和原生的 call 操作进行比较验证
expect(bar._call(foo)).toBe(bar.call(foo));
});
it('change the direction of this, use in constructor', () => {
function Product(name, price) {
this.name = name;
this.price = price;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
function Food2(name, price) {
Product._call(this, name, price);
this.category = 'food';
}
// 和原生的 call 操作进行比较验证
expect(new Food2('cheese', 5).name).toBe(new Food('cheese', 5).name);
});
it('when \'this\' argument is null or undefined', () => {
window.value = 2;
function bar() {
return this.value;
}
// 这是非严格模式下的运行结果,严格模式下会报错
expect(bar._call(null)).toBe(2);
expect(bar._call(undefined)).toBe(2);
});
it('when \'this\' is other primitive value, excute success', () => {
function bar() {
return this.length;
}
// 和原生的 call 操作进行比较验证
expect(bar._call('123')).toBe(bar.call('123'));
});
});