Skip to content

Commit 393868b

Browse files
chore(*): Update PhantomJS bind shim to use es5-shim's version
1 parent 8d34108 commit 393868b

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

test/commonSpec.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,44 @@ var common = uiRouter.common,
1616
* Because PhantomJS sucks...
1717
*/
1818
if (!Function.prototype.bind) {
19-
Function.prototype.bind = function(oThis) {
20-
if (typeof this !== 'function') {
21-
// closest thing possible to the ECMAScript 5
22-
// internal IsCallable function
23-
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
19+
var Empty = function(){};
20+
Function.prototype.bind = function bind(that) { // .length is 1
21+
var target = this;
22+
if (typeof target != "function") {
23+
throw new TypeError("Function.prototype.bind called on incompatible " + target);
2424
}
25+
var args = Array.prototype.slice.call(arguments, 1); // for normal call
26+
var binder = function () {
27+
if (this instanceof bound) {
28+
var result = target.apply(
29+
this,
30+
args.concat(Array.prototype.slice.call(arguments))
31+
);
32+
if (Object(result) === result) {
33+
return result;
34+
}
35+
return this;
36+
} else {
37+
return target.apply(
38+
that,
39+
args.concat(Array.prototype.slice.call(arguments))
40+
);
41+
}
42+
};
43+
var boundLength = Math.max(0, target.length - args.length);
44+
var boundArgs = [];
45+
for (var i = 0; i < boundLength; i++) {
46+
boundArgs.push("$" + i);
47+
}
48+
var bound = Function("binder", "return function(" + boundArgs.join(",") + "){return binder.apply(this,arguments)}")(binder);
2549

26-
var aArgs = Array.prototype.slice.call(arguments, 1),
27-
fToBind = this,
28-
fNOP = function() {},
29-
fBound = function() {
30-
return fToBind.apply(this instanceof fNOP && oThis
31-
? this
32-
: oThis,
33-
aArgs.concat(Array.prototype.slice.call(arguments)));
34-
};
35-
36-
fNOP.prototype = this.prototype;
37-
fBound.prototype = new fNOP();
38-
39-
return fBound;
50+
if (target.prototype) {
51+
Empty.prototype = target.prototype;
52+
bound.prototype = new Empty();
53+
// Clean up dangling references.
54+
Empty.prototype = null;
55+
}
56+
return bound;
4057
};
4158
}
4259

0 commit comments

Comments
 (0)