Function.prototype.myBind = function (ctx, ...rest) {
if (typeof this !== "function") {
throw new Error("myBind is only used to Function");
}
const _this = this;
return function fn(...args) {
// 拼接参数
const newArgs = rest.concat(args);
// 外部有可能 new fn();
if (this instanceof fn) {
return new _this(newArgs);
}
return this.apply(ctx, newArgs);
};
};