Function.prototype.call = function(context) {
context = context || window
context.fn = this
const args = [...arguments].slice(1)
const result = context.fn(...args)
delete context.fn
return result
}
手写apply
Function.prototype.apply = function(context) {
context = context || window
context.fn = this
const args = arguments[1]
if(args) {
const result = context.fn(...args)
} else{
const result = context.fn()
}
delete context.fn
return result
}
手写bind
//bind返回的函数如果作为构造函数,搭配new关键字出现的话,我们绑定的this就需要被忽略
//处理构造函数场景下的兼容
Function.prototype.bind = Function.prototype.bind || function(context){
//确保调用bind方法的一定要是一个函数
if(typeof this !== "function"){
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var args = Array.prototype.slice.call(arguments, 1);
var self = this;
var F = function(){};
F.prototype = this.prototype;
var bound = function(){
var innerArgs = Array.prototype.slice.call(arguments);
var finalArgs = args.concat(innerArgs);
return self.apply(this instanceof F ? this : context || this, finalArgs);
}
bound.prototype = new F();
return bound;
}