-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallApplyBind.js
61 lines (55 loc) · 1.67 KB
/
CallApplyBind.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
57
58
59
60
61
/*
这里需要使用Symbol 来确定该this 是thisArg中的唯一,不会重复 this 为调用call,bind,apply的方法 例如 testBind.say()._myBind() =》 this 为 testBind.say
*/
Function.prototype._myCall = function (thisArg, ...args) {
thisArg =
thisArg !== null && thisArg !== undefined ? Object(thisArg) : Window;
let fn = Symbol();
thisArg[fn] = this; //这个this
const res = thisArg[fn](...args);
delete thisArg[fn];
return res;
};
Function.prototype._myApply = function (thisArg, args = []) {
thisArg =
thisArg !== null && thisArg !== undefined ? Object(thisArg) : window;
let fn = Symbol();
thisArg[fn] = this;
const res = thisArg[fn](...args);
delete thisArg[fn];
return res;
};
Function.prototype._myBind = function (thisArg, ...args) {
console.log(
this
); /* 打印结果: ;ƒ say(prefix, age) {console.log(`${prefix},my name is ${this.name},i am ${age} year old`);} */
thisArg =
thisArg !== null && thisArg !== undefined ? Object(thisArg) : window;
const fn = Symbol();
thisArg[fn] = this;
return function (...innerArgs) {
return thisArg[fn](...args, ...innerArgs);
};
};
// test
const testBind = {
name: "麻不烧",
say(prefix, age) {
console.log(`${prefix},my name is ${this.name},i am ${age} year old`);
},
};
const B = {
name: "小丁丁",
};
const sayB = testBind.say._myBind(B, "hello");
sayB(3); // 'hello,my name is 小丁丁,i am 3 year old''
const testCall = {
name: "麻不烧",
say(prefix, age) {
console.log(`${prefix},my name is ${this.name},i am ${age} year old`);
},
};
const A = {
name: "小丁",
};
testCall.say._myCall(A, "hello", 3); // 'hello,my name is 小丁,i am 3 year old'