1 var baseRest = require('./_baseRest'),
2 createWrap = require('./_createWrap'),
3 getHolder = require('./_getHolder'),
4 replaceHolders = require('./_replaceHolders');
6 /** Used to compose bitmasks for function metadata. */
7 var WRAP_BIND_FLAG = 1,
8 WRAP_BIND_KEY_FLAG = 2,
9 WRAP_PARTIAL_FLAG = 32;
12 * Creates a function that invokes the method at `object[key]` with `partials`
13 * prepended to the arguments it receives.
15 * This method differs from `_.bind` by allowing bound functions to reference
16 * methods that may be redefined or don't yet exist. See
17 * [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern)
20 * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic
21 * builds, may be used as a placeholder for partially applied arguments.
27 * @param {Object} object The object to invoke the method on.
28 * @param {string} key The key of the method.
29 * @param {...*} [partials] The arguments to be partially applied.
30 * @returns {Function} Returns the new bound function.
35 * 'greet': function(greeting, punctuation) {
36 * return greeting + ' ' + this.user + punctuation;
40 * var bound = _.bindKey(object, 'greet', 'hi');
44 * object.greet = function(greeting, punctuation) {
45 * return greeting + 'ya ' + this.user + punctuation;
51 * // Bound with placeholders.
52 * var bound = _.bindKey(object, 'greet', _, '!');
56 var bindKey = baseRest(function(object, key, partials) {
57 var bitmask = WRAP_BIND_FLAG | WRAP_BIND_KEY_FLAG;
58 if (partials.length) {
59 var holders = replaceHolders(partials, getHolder(bindKey));
60 bitmask |= WRAP_PARTIAL_FLAG;
62 return createWrap(key, bitmask, object, partials, holders);
65 // Assign default placeholders.
66 bindKey.placeholder = {};
68 module.exports = bindKey;