HTML and CSS Reference
In-Depth Information
var args = slice.call(arguments, 1);
return function () {
var allArgs = args;
if (arguments.length > 0) {
allArgs = args.concat(slice.call(arguments));
}
return target.apply(thisObj, allArgs);
};
}
return function () {
if (arguments.length > 0) {
return target.apply(thisObj, arguments);
}
return target.call(thisObj);
};
};
}());
}
This implementation is somewhat more involved, but yields much better per-
formance, especially for the simple case of binding a function to an object and no
arguments and calling it with no arguments.
Note that the implementation given here is missing one feature from the EC-
MAScript 5 specification. The spec states that the resulting function should behave
as the bound function when used in a
new
expression.
6.1.5 Currying
Currying is closely related to binding, because they both offer a way to partially
apply a function. Currying differs from binding in that it only pre-fills arguments; it
does not set the
this
value. This is useful, because it allows us to bind arguments
to functions and methods while maintaining their implicit
this
value. The implicit
this
allows us to use currying to bind arguments to functions on an object's proto-
type, and still have the function execute with a given object as its
this
value. Listing
6.8 shows an example of implementing
String.prototype.trim
in terms of
String.prototype.replace
using
Function.prototype.curry
.