@samitouri / QOS-React-2 / commits / 507e58ed96
Fork 0

The library for web and native user interfaces.

Pull in my rewritten Function.prototype.bind polyfill from upstream.

We don't sync upstream polyfills (because we don't have a story for how they would be used), so this needs to be updated manually. Sacrificed some negligible performance optimizations to reduce the number of different cases from four to one. It's important to test this implementation in PhantomJS, since that's the only browser that I know of where built-in functions sometimes do not have a `.prototype`.

Ben Newman committed Jul 19, 2013 at 11:02 UTC 507e58ed9661bbfd7e89ad19460d3c80a6cf572e
1 file changed +15 -31
src/test/all.js
+15 -31
@@ -12,40 +12,24 @@ if (!Fp.bind) {
12 Fp.bind = function(context) {
13 var func = this;
14 var args = slice.call(arguments, 1);
15 - var bound;
15
17 - if (func.prototype) {
18 - if (args.length > 0) {
19 - bound = function() {
20 - return func.apply(
21 - !(this instanceof func) && context || this,
22 - args.concat(slice.call(arguments))
23 - );
24 - };
25 - } else {
26 - bound = function() {
27 - return func.apply(
28 - !(this instanceof func) && context || this,
29 - arguments
30 - );
31 - };
32 - }
33 -
34 - bound.prototype = Object.create(func.prototype);
35 -
36 - } else if (args.length > 0) {
37 - bound = function() {
38 - return func.apply(
39 - context || this,
40 - args.concat(slice.call(arguments))
41 - );
42 - };
43 - } else {
44 - bound = function() {
45 - return func.apply(context || this, arguments);
46 - };
16 + function bound() {
17 + var invokedAsConstructor = func.prototype && (this instanceof func);
18 + return func.apply(
19 + // Ignore the context parameter when invoking the bound function
20 + // as a constructor. Note that this includes not only constructor
21 + // invocations using the new keyword but also calls to base class
22 + // constructors such as BaseClass.call(this, ...) or super(...).
23 + !invokedAsConstructor && context || this,
24 + args.concat(slice.call(arguments))
25 + );
26 }
27
28 + // The bound function must share the .prototype of the unbound
29 + // function so that any object created by one constructor will count
30 + // as an instance of both constructors.
31 + bound.prototype = func.prototype;
32 +
33 return bound;
34 };
35 }