1.手写call,apply,bind
Function.prototype.myCall = function(context){
context = context || window
context.fn = this
let arg = [...arguments].slice(1)
let result = context.fn(...arg)
delete context.fn
return result
}
Function.prototype.myApply = function(context){
context = context || window
context.fn = this
let result
if(arguments[1]){
result = context.fn(...arguments[1])
}else{
result = context.fn()
}
delete context.fn
return result
}
Function.prototype.myBind = function(thisArg){
if(typeof this !== 'function') return
let that = this
let arg = [...arguments].slice(1)
let fnNop = function(){}
let fn = function(){
return that.apply(this instanceof fn ? this : thisArg , arg.concat([...arguments]))
}
if(this.prototype){
fnNop.prototype = this.prototype
}
fn.prototype = new fnNop()
return fn
}