Function bind polyfill

A polyfill function for ES5 Function.prototype.bind:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (!Function.prototype.bind) {
Function.prototype.bind = function (obj) {
var slice = [].slice;
var args = slice.call(arguments, 1);
var self = this;
var nop = function () {};
var bound = function () {
return self.apply(
this.instanceof nop ? this : (obj || {}),
args.concat(slice.call(arguments))
);
nop.prototype = self.prototype;
bound.prototype = new nop();
return bound;
};
}
// fn.bind(obj}) => obj.bound => obj.bound(args)
// Ctrl.bind(new Ctrl())
0%