如何手写call、apply、bind?
callFunction.prototype.call(this, arg1, arg2, …..)可以改变this,并且传入参数,立刻执行,返回函数返回值手写callFunction.prototype.myCall = function(context = window, …args) { context = context || window; // 参数默认值并不会排除null,所以重新赋值 context.fn = this; // this是调用call的函数 const result = context.fn(…args); delete context.fn; // 执行后删除新增属性 return result;}applyFunction.prototype.apply(this, [arg1, arg2, …..])可以改变this,并且传入参数,与call不同的是,传入的参数是数组或类数组,立刻执行,返回函数返回值手写apply:Function.prototype.myApply = function(context = window, args = []) { context = context || window; // 参数默认值并不会排除null,所以重新赋值 context.fn = this; // this是调用call的函数 const result = context.fn(…args); delete context.fn; return result;}bindFunction.prototype.bind(this, arg1, arg2, …)可以绑定this,并且传入参数,方式与call相同,但是不会执行,返回已绑定this的新函数手写bind:Function.prototype.myBind = function(context, …args) { const _this = this; return function Bind(…newArgs) { // 考虑是否此函数被继承 if (this instanceof Bind) { return _this.myApply(this, […args, …newArgs]) } return _this.myApply(context, […args, …newArgs]) }} ...