SOURCE

function foo(name){
    console.log('this.a:',this.a)
    // console.log(...arguments)
    this.name = name
}
foo.prototype.sex = 'man';

const bar = {  a: 'bar' }

Function.prototype.bind = function(){
    const self = this // [fn].bind
    const args = [...arguments]
    const ctx = args.shift()
    function fBound() {
        // 判断是否通过 new bindFn() 调用,
        // 非构造函数则使用fn.bind(ctx)中的上下午对象
        // 构造函数则使用构造函数产生的 this
        const otherArgs = [...arguments]
        return self.apply(this instanceof fBound ? this : ctx, args.concat(otherArgs))
    }
    fBound.prototype = this.prototype
    return fBound
}

const bindFoo = foo.bind(bar, 1, 2)
// bindFoo(3)
const newBind = new bindFoo('bind')
console.log(newBind.name)
console.log(newBind.sex)

// console.log('newBind.a:', newBind.a)
console 命令行工具 X clear

                    
>
console