call/bind
Function.prototype.myCall = function (context) {
if (context === null || context === undefined) {
context = window
} else {
context = Object(context)
}
const args = [...arguments].slice(1)
const fn = SymBol();
context[fn] = this;
const result = context[fn](...arr);
delete context[fn];
return result
}
Function.prototype.myCall2 = function (context) {
var context = context || window;
context.fn = this;
var args = [];
for(var i = 1, len = arguments.length; i < len; i++) {
args.push('arguments[' + i + ']');
}
var result = eval('context.fn(' + args +')');
delete context.fn
return result;
}
Function.prototype.myApply = function (context, arr) {
if (context === null || context === undefined) {
context = window
} else {
context = Object(context)
}
const fn = SymBol();
context[fn] = this;
const result = context[fn](...arr);
delete context[fn];
return result
}
Function.prototype.myApply2 = function (context, arr) {
var context = Object(context) || window;
context.fn = this;
var result;
if (!arr) {
result = context.fn();
}
else {
var args = [];
for (var i = 0, len = arr.length; i < len; i++) {
args.push('arr[' + i + ']');
}
result = eval('context.fn(' + args + ')')
}
delete context.fn
return result;
}