-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.js
24 lines (22 loc) · 832 Bytes
/
apply.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
Function.prototype.customApplyEs5 = function() {
if (typeof this !== "function") throw new Error("is not function");
var contnet = arguments[0];
var args = arguments[1];
var str = "contnet._" + this.name + "(";
var i = 0;
var res = null;
contnet["_" + this.name] = this;
for (i; i < args.length; i++) str += args[i] + (args.length - 1 === i ? ")" : ",");
res = eval(str);
delete contnet["_" + this.name];
return res;
};
Function.prototype.customApplyEs6 = function(contnet, args) {
if (typeof this !== "function") throw new Error("is not function");
contnet[`_${this.name}`] = this;
const res = contnet[`_${this.name}`](...args);
delete contnet[`_${this.name}`];
return res;
};
console.log([].slice.customApplyEs5([1, 2, 3, 4], [1, 3]));
console.log([].slice.customApplyEs6([1, 2, 3, 4], [1, 3]));