Function.prototype.newBind = function(_this, ...prependArgs){ // bind函数接收一个改变的this参数和其他剩余预制参数
let self = this;
return function(...args) { //bind函数返回一个function, 这个function调用时也可以接收参数
self.call(_this, ...prependArgs, ...args) //执行函数,并且改变函数的this指向,并且将置制参数和调用时的参数都传入
}
}
Function.prototype.newCall = function(_this, ...args) { // 剩余参数捕获
// 总体思路是把function 挂到 _this 上,改变this指向, 同时执行_this.fn
var context = _this || window;
context.fn = this; // 这个this是function, 因为调用时是 fn.newCall(obj)
context.fn(...args); // 执行函数,改变函数的this指向
delete context.fn
};
Function.prototype.newApply = function(_this, args) {
// 总体思路是把function 挂到 _this 上,改变this指向, 同时执行_this.fn
var context = _this || window;
context.fn = this;
context.fn(...args); // 执行函数,改变函数的this指向
delete context.fn
};