重庆网站开发服务,网站建设问题新闻资讯,牡丹江商城网站建设,夜月直播下载直播因为 bind 的使用方法是 某函数.bind(某对象#xff0c;...剩余参数) 所以需要在 Function.prototype 上进行编程将传递的参数中的某对象和剩余参数使用 apply 的方式在一个回调函数中执行即可要在第一层获取到被绑定函数的 this#xff0c;因为要拿到那个函数用 apply
/***…因为 bind 的使用方法是 某函数.bind(某对象...剩余参数) 所以需要在 Function.prototype 上进行编程将传递的参数中的某对象和剩余参数使用 apply 的方式在一个回调函数中执行即可要在第一层获取到被绑定函数的 this因为要拿到那个函数用 apply
/*** 简单版本*/
Function.prototype.myBind (that, ...args) {const funcThis this;return function (..._args) {return funcThis.apply(that, args.concat(_args));};
};
自封装一个 apply
首先要先原型上即 Function.prototype 上编程需要拿到函数的引用 在这里是 this让 传入对象.fn this执行 传入对象.fn(传入参数)返回执行结果
Function.prototype.myApply function (context) {if (typeof this ! function) {throw new TypeError(Error);}context context || window;context.fn this;let result;// 处理参数和 call 有区别if (arguments[1]) {result context.fn(...arguments[1]);} else {result context.fn();}delete context.fn;return result;
};