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_PARTIAL_FLAG = 32;
11 * Creates a function that invokes `func` with the `this` binding of `thisArg`
12 * and `partials` prepended to the arguments it receives.
14 * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds,
15 * may be used as a placeholder for partially applied arguments.
17 * **Note:** Unlike native `Function#bind`, this method doesn't set the "length"
18 * property of bound functions.
24 * @param {Function} func The function to bind.
25 * @param {*} thisArg The `this` binding of `func`.
26 * @param {...*} [partials] The arguments to be partially applied.
27 * @returns {Function} Returns the new bound function.
30 * function greet(greeting, punctuation) {
31 * return greeting + ' ' + this.user + punctuation;
34 * var object = { 'user': 'fred' };
36 * var bound = _.bind(greet, object, 'hi');
40 * // Bound with placeholders.
41 * var bound = _.bind(greet, object, _, '!');
45 var bind = baseRest(function(func, thisArg, partials) {
46 var bitmask = WRAP_BIND_FLAG;
47 if (partials.length) {
48 var holders = replaceHolders(partials, getHolder(bind));
49 bitmask |= WRAP_PARTIAL_FLAG;
51 return createWrap(func, bitmask, thisArg, partials, holders);
54 // Assign default placeholders.
55 bind.placeholder = {};
57 module.exports = bind;