// call
Function.prototype.myCall = function () {
[context, ...arg] = [...arguments];
context.fn = this;
var result = context.fn(...arg);
delete context.fn;
return result;
}
// apply
Function.prototype.myApply = function (context, args) {
context.fn = this;
var result = context.fn(...args);
delete context.fn;
return result;
}
// bind
Function.prototype.newBind = function (newThis) {
var self = this;
var args = [].slice.call(arguments, 1);
var func = function () {
var _args = [].slice.call(arguments, 0);
return self.apply(this instanceof func ? self : (newThis || window), args.concat(_args));
}
return func;
}
call、apply、bind
最新推荐文章于 2024-11-14 17:44:13 发布