-
Notifications
You must be signed in to change notification settings - Fork 20
/
function-bind-polyfill.js
39 lines (34 loc) · 980 Bytes
/
function-bind-polyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
if (!Function.prototype.bind) {
/* eslint-disable no-extend-native */
Function.prototype.bind = function bind(that) { // .length is 1
var self = this;
if (typeof self !== 'function') {
throw new TypeError('Function.prototype.bind called on incompatible ' + self);
}
var args = Array.prototype.slice.call(arguments, 1); // for normal call
var bound = function () {
if (this instanceof bound) {
var result = self.apply(
this,
args.concat(Array.prototype.slice.call(arguments))
);
if (Object(result) === result) {
return result;
}
return this;
} else {
return self.apply(
that,
args.concat(Array.prototype.slice.call(arguments))
);
}
};
if (self.prototype) {
var Empty = function () {
};
Empty.prototype = self.prototype;
bound.prototype = new Empty();
}
return bound;
};
}