Skip to content

Commit 4d9643b

Browse files
committed
Merge pull request testdouble#76 from UncleGene/bind
Better compatible shim for Function.prototype.bind
2 parents ff63c2b + 182e2db commit 4d9643b

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
(function() {
22
/**
3-
* Function.bind Polyfill for ECMAScript 5 Support
4-
* Kangax's bind with Broofa's arg optimization.
5-
* http://www.broofa.com/Tools/JSLitmus/tests/PrototypeBind.html
3+
* Function.bind for ECMAScript 5 Support
64
*
7-
* Copied from https://gist.github.com/rxgx/1597825
5+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
86
*/
9-
if (typeof Function.prototype.bind !== "function") {
10-
Function.prototype.bind = function() {
11-
var slice = Array.prototype.slice;
12-
return function(context) {
13-
var fn = this,
14-
args = slice.call(arguments, 1);
15-
if (args.length) {
16-
return function() {
17-
return arguments.length
18-
? fn.apply(context, args.concat(slice.call(arguments)))
19-
: fn.apply(context, args);
7+
8+
if (!Function.prototype.bind) {
9+
Function.prototype.bind = function (oThis) {
10+
if (typeof this !== "function") {
11+
// closest thing possible to the ECMAScript 5 internal IsCallable function
12+
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
13+
}
14+
15+
var aArgs = Array.prototype.slice.call(arguments, 1),
16+
fToBind = this,
17+
fNOP = function () {},
18+
fBound = function () {
19+
return fToBind.apply(this instanceof fNOP && oThis
20+
? this
21+
: oThis,
22+
aArgs.concat(Array.prototype.slice.call(arguments)));
2023
};
21-
}
22-
return function() {
23-
return arguments.length
24-
? fn.apply(context, arguments)
25-
: fn.call(context);
26-
};
27-
};
24+
25+
fNOP.prototype = this.prototype;
26+
fBound.prototype = new fNOP();
27+
28+
return fBound;
2829
};
2930
}
3031
})();
31-

0 commit comments

Comments
 (0)