bind
if (!Function.prototype.bind) {
Function.prototype.bind = function (targetObj, ...args) {
const self = this;
const f = function (...rest) {
return self.apply(this instanceof f ? this : (targetObj || window), ...args, ...rest)
}
f.prototype = Object.create(this.prototype)
return f
}
}
softbind
if (!Function.prototype.softBind) {
Function.prototype.softBind = function(obj, ...args) {
const fn = this
const bound = function(...rest) {
const o = !this || this === (window || global) ? obj : this
return fn.apply(o, [...args, ...rest])
}
bound.prototype = Object.create(this