Function.prototype.mybind = function (context) {
// 保存是谁调用的函数
let self = this
let arg1 = [...arguments].slice(1)
let ret = function (...arg2) {
// 要处理调用bind之后函数再被new的情况
// 在new的情况下,函数里面的this将会被指向新的obj,obj.__proto__指向的是
// 构造函数的原型,也就是ret.prototype,通过instanceof判断是否是new对象
self.apply(this instanceof ret ? this : context, [...arg1, ...arg2])
}
// 保持原型链,即新的对象要继承调用bind这个函数的原型,fn是为了隔离他们
// 因为对象的引用性,改则都改
function fn() { }
ret.prototype = new fn()
fn.prototype = this.prototype
return ret
}