-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyfill.js
71 lines (51 loc) · 2.28 KB
/
polyfill.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
62
63
64
65
66
67
68
69
70
71
/*
Without calling the Function.Prototype.call, implement myCall function on Function prototype.
myCall(thisContext, ...args) - should call original function with thiscontext bound to function's this keyword, passing all the remaing arguments as individual
arguments to the function.
myApply(thisContext, ...args) - should call original function with thiscontext bound to function's this keyword, passing all the remaing arguments as an array of individual
arguments to the function.
myApply(thisContext, ...args) - should return a new function that calls the original function with thisContext bound to the function's this keyword, passing all of the remaining args
as individual arguments to the function. The new function should accept optional arguments, which should also be passed to the original function, after the
args originally passed to myBind.
*/
const obj = {num: 0};
function logNums(x,y) {
console.log(this.num, x, y);
}
Function.prototype.myCall = function (thisContext, ...args) {
const symbol = Symbol();
let originalFunc = this; // when we are inside of prototype, the this keyword will be bound on the actual object we are on, in this case, the function we are on or trying to call
if (typeof originalFunc !== "function") {
throw new TypeError("Bind must be called on a function");
}
thisContext[symbol] = originalFunc;
const val = thisContext[symbol](...args);
delete thisContext[symbol];
return val;
};
logNums.call(obj, 1, 2); // original call method on function prototype.
logNums.myCall(obj, 2, 4) //polyfill
Function.prototype.myApply = function (thisContext, args = []) {
const symbol = Symbol();
let originalFunc = this;
if (typeof originalFunc !== "function") {
throw new TypeError("Bind must be called on a function");
}
thisContext[symbol] = originalFunc;
const val = thisContext[symbol](...args);
delete thisContext[symbol];
return val;
};
Function.prototype.myBind = function (thisContext, ...args) {
return (...args2) => {
const symbol = Symbol();
let originalFunc = this;
if (typeof originalFunc !== "function") {
throw new TypeError("Bind must be called on a function");
}
thisContext[symbol] = originalFunc;
const val = thisContext[symbol](...args, ...args2);
delete thisContext[symbol];
return val;
}
}