diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index c89dbb554..38ef58113 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -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(); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 26e6a56c3..edabe3217 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -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;