通过bind来绑定函数的this称为硬绑定,硬绑后的函数会带来一个问题:无论怎样都不会再改变this指向了(不能通过显示绑定和隐式绑定来改变this指向)。
软绑定就是为了解决这个问题,如下是一个简单的软绑例子
Function.prototype.softBind = function (obj) {// 这里没有实现bind的柯里化功能,可以自行添加
var fn = this;
return function () {
return fn.apply((!this || this == window) ? obj : this, arguments)
}
}
var a=function(){console.log(this)}
var b=a.softBind(1)
b();
b.call(2)