SOURCE

var a = { name: 'a' };
var b = { name: 'b' };
var fun = function () { console.log(this.name) };
var fun1 = fun.bind(a);
var fun2 = fun1.bind(b);
fun1(); //a
fun2(); //a

// 实现bind
Function.prototype._bind = function (thisObj) {
    const self = this;
    const args = [...arguments].slice(1)
    return function () {
        const finalArgs = [...args, ...arguments]
        self.apply(thisObj, finalArgs);
    }
}

Function.prototype.myBind = function (target) {
    let self = this

    let args = Array.prototype.slice.call(arguments, 1)

    let fNOP = function () {}

    const fBound = function () {
        let bindArgs = Array.prototype.slice.call(arguments)
        return self.apply(this instanceof fNOP ? this : target, args.concat(bindArgs))
    }
    
    fNOP.prototype = self.prototype
    fBound.prototype = new fNOP()
    return fBound

}
console 命令行工具 X clear

                    
>
console