Skip to content

Commit 603c9ef

Browse files
committed
Implement constructor-aware binding per @zpao's request.
When the function to be bound does not have a prototype, ignore the constructor case.
1 parent 906b8f3 commit 603c9ef

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/test/all.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,41 @@ if (!Fp.bind) {
1212
Fp.bind = function(context) {
1313
var func = this;
1414
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;
2350
};
2451
}
2552

0 commit comments

Comments
 (0)