Skip to content

Commit 63e5a22

Browse files
author
Eric Wendelin
committed
Fix Function.bind polyfill.
1 parent b9c92c0 commit 63e5a22

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

polyfills.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
// ES5 Polyfills
22
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
33
if (!Function.prototype.bind) {
4-
Function.prototype.bind = function (oThis) {
5-
if (typeof this !== 'function') {
6-
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
7-
}
8-
9-
var aArgs = Array.prototype.slice.call(arguments, 1);
10-
var fToBind = this;
11-
var NoOp = function () {
12-
};
13-
var fBound = function () {
14-
return fToBind.apply(this instanceof NoOp && oThis ? this : oThis,
15-
aArgs.concat(Array.prototype.slice.call(arguments)));
4+
Function.prototype.bind = function bind(obj) {
5+
var args = slice.call(arguments, 1);
6+
var self = this;
7+
var F = function() {};
8+
var bounded = function() {
9+
return self.apply(
10+
this instanceof F ? this : (obj || {}),
11+
args.concat(slice.call(arguments))
12+
);
1613
};
17-
18-
NoOp.prototype = this.prototype;
19-
fBound.prototype = new NoOp();
20-
21-
return fBound;
14+
F.prototype = this.prototype || {};
15+
bounded.prototype = new F();
16+
return bounded;
2217
};
2318
}
2419

25-
2620
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
2721
if (!Array.prototype.map) {
2822
Array.prototype.map = function(callback, thisArg) {

0 commit comments

Comments
 (0)