手写bind

97 阅读1分钟
Function.prototype.mybind = function (...arguments) {
    const fn = this;
    let ref = arguments[0];
    if (typeof ref === 'object') {
        ref = ref || window;
    } else {
        ref = Object.create(null);
    }
    let argument1 = arguments.slice(1);

    return function (...arguments2) {
        const arguments = argument1.concat(arguments2);
        return fn.apply(ref, arguments);
    };
};
var test = function (a, b, c) {
    console.log('x=' + this.x, 'a=' + a, 'b=' + b, 'c=' + c);
};
var o = {
    x: 1
};
test.mybind(o, 1)(2, 3); //x=1 a=1 b=2 c=3