added _.bindKey definition

This commit is contained in:
Brian Zengel
2013-10-18 22:30:05 -04:00
parent 12c0a1ce72
commit c9437135dd
2 changed files with 31 additions and 4 deletions
+17 -3
View File
@@ -393,11 +393,11 @@ _.forEach(saves, function(type) {
asyncSave({ 'type': type, 'complete': done });
});
var func = function (greeting) { return greeting + ' ' + this.name };
var funcBind = function (greeting) { return greeting + ' ' + this.name };
// need a second var otherwise typescript thinks func signature is the above func type,
// instead of the newly returned _bind => func type.
var func2 = _.bind(func, { 'name': 'moe' }, 'hi');
func2();
var funcBind2 = _.bind(funcBind, { 'name': 'moe' }, 'hi');
funcBind2();
var view = {
'label': 'docs',
@@ -407,7 +407,21 @@ var view = {
_.bindAll(view);
jQuery('#docs').on('click', view.onClick);
var objectBindKey = {
'name': 'moe',
'greet': function(greeting) {
return greeting + ' ' + this.name;
}
};
var funcBindKey = _.bindKey(object, 'greet', 'hi');
funcBindKey();
objectBindKey.greet = function(greeting) {
return greeting + ', ' + this.name + '!';
};
funcBindKey();
+14 -1
View File
@@ -1731,7 +1731,20 @@ declare module _ {
object: any,
...methodNames: string[]): any;
/**
* Creates a function that, when called, invokes the method at object[key] and prepends any
* additional bindKey arguments to those provided to the bound function. This method differs
* from _.bind by allowing bound functions to reference methods that will be redefined or don't
* yet exist. See http://michaux.ca/articles/lazy-function-definition-pattern.
* @param object The object the method belongs to.
* @param key The key of the method.
* @param args Arguments to be partially applied.
* @return The new bound function.
**/
export function bindKey(
object: any,
key: string,
...args: any[]): Function;