// apply与call相似,只有第二个参数是一个数组,Function.prototype.Apply=function(context, args){if(!context || context ===null){
context = window
}let fn =Symbol()
context[fn]=thisconst res = context[fn](...args)delete context[fn];return res
}
三、手写bind
Function.prototype.Bind=function(context,...args){if(!context || context ===null){
context = window
}let fn =thislet f =Symbol()constresult=function(...args1){if(thisinstanceoffn){// result 如果作为构造函数被调用, ths 指向的是 new 出来的对象// this instanceof fn,判断 new 出来的对象是否为 fn 的实例this[f]= fn
let res =this[f](...args,...args1)deletethis[f]return res
}else{// bind 返回的函数作为普通函数被调用时
context[f]= fn
let res = context[f](...args,...args1)delete context[f]return res
}}// 如果绑定的是构造函数 那么需要继承构造函数原型属性和方法// 实现继承的方式: 使用 Object.create
result.prototype = Object.create(fn.prototype);return result;}