Function.prototype.__bind__ = function (context = window, ...args_front) {
if (typeof this !== 'function') throw new TypeError('You can not use call() at a non-function')
const fSelf = this
const fEmpty = function () {
}
const fBound = function (...args_back) {
if (this instanceof fEmpty) {
Object.setPrototypeOf(this, fSelf.prototype) //重新指向源构造函数
return fSelf.apply(this, ...args_front.concat(args_back))
}
return fSelf.apply(context, ...args_front.concat(args_back))
}
fEmpty.prototype = fSelf.prototype
fBound.prototype = new fEmpty()
return fBound
}