@samitouri / QOS-React-2 / commits / 603c9ef6a8
Fork 0

The library for web and native user interfaces.

Implement constructor-aware binding per @zpao's request.

When the function to be bound does not have a prototype, ignore the constructor case.

Ben Newman committed Jun 3, 2013 at 13:18 UTC 603c9ef6a8d70d3cf29ee9d0a9d7969abce48ac4
1 file changed +35 -8
src/test/all.js
+35 -8
@@ -12,14 +12,41 @@ if (!Fp.bind) {
12 Fp.bind = function(context) {
13 var func = this;
14 var args = slice.call(arguments, 1);
15 - return args.length > 0 ? function() {
16 - return func.apply(
17 - context || this,
18 - args.concat(slice.call(arguments))
19 - );
20 - } : function() {
21 - return func.apply(context || this, arguments);
22 - };
15 + var bound;
16 +
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 + };
47 + }
48 +
49 + return bound;
50 };
51 }
52