Function.prototype.myapply = function (context, ...argus) {
if (typeof this !== 'function') {
throw new TypeError('not funciton')
}
const fn = this
let result = null
context = context || window
argus = argus && argus[0] || []
context.fn = fn
result = context.fn(...argus)
delete context.fn
return result
}
Function.prototype.mybind = function (context) {
var me = this;
var args = Array.prototype.slice.myapply(arguments, [1]);
var F = function () {};
F.prototype = this.prototype;
var bound = function () {
var innerArgs = Array.prototype.slice.mycall(arguments);
var finalArgs = args.concat(innerArgs);
return me.myapply(this instanceof F ? this : context || this, finalArgs);
}
bound.prototype = new F();
return bound;
}
手写bind apply 方法
最新推荐文章于 2022-06-19 19:20:47 发布