bind polyfill
for education
JavaScript
function bind() {
var func = this,
obj = arguments[0],
args = Array.prototype.slice.call(arguments, 1);
return function() {
var argumnts = args.concat(Array.prototype.slice.call(arguments));
return func.apply(obj, argumnts);
}
}
Function.prototype.bindCustom = bind;
var a = {
foo: 1,
bar: function(a1,a2,a3) {
console.log(this.foo+a1+a2+a3);
}
};
var c = a.bar.bindCustom(a,2);
c(3,4);