1. Function.prototype.myBind = function (ctx, ...rest) {
    2. if (typeof this !== "function") {
    3. throw new Error("myBind is only used to Function");
    4. }
    5. const _this = this;
    6. return function fn(...args) {
    7. // 拼接参数
    8. const newArgs = rest.concat(args);
    9. // 外部有可能 new fn();
    10. if (this instanceof fn) {
    11. return new _this(newArgs);
    12. }
    13. return this.apply(ctx, newArgs);
    14. };
    15. };