Merge pull request #210 from benjamn/rewrite-Function.prototype.bind-polyfill

Pull in my rewritten Function.prototype.bind polyfill from upstream
This commit is contained in:
Paul O’Shannessy
2013-07-19 10:23:17 -07:00
+15 -31
View File
@@ -12,40 +12,24 @@ if (!Fp.bind) {
Fp.bind = function(context) {
var func = this;
var args = slice.call(arguments, 1);
var bound;
if (func.prototype) {
if (args.length > 0) {
bound = function() {
return func.apply(
!(this instanceof func) && context || this,
args.concat(slice.call(arguments))
);
};
} else {
bound = function() {
return func.apply(
!(this instanceof func) && context || this,
arguments
);
};
}
bound.prototype = Object.create(func.prototype);
} else if (args.length > 0) {
bound = function() {
return func.apply(
context || this,
args.concat(slice.call(arguments))
);
};
} else {
bound = function() {
return func.apply(context || this, arguments);
};
function bound() {
var invokedAsConstructor = func.prototype && (this instanceof func);
return func.apply(
// Ignore the context parameter when invoking the bound function
// as a constructor. Note that this includes not only constructor
// invocations using the new keyword but also calls to base class
// constructors such as BaseClass.call(this, ...) or super(...).
!invokedAsConstructor && context || this,
args.concat(slice.call(arguments))
);
}
// The bound function must share the .prototype of the unbound
// function so that any object created by one constructor will count
// as an instance of both constructors.
bound.prototype = func.prototype;
return bound;
};
}