1 |
3a515b92
|
cagy
|
var baseRest = require('./_baseRest'),
|
2 |
|
|
createWrap = require('./_createWrap'),
|
3 |
|
|
getHolder = require('./_getHolder'),
|
4 |
|
|
replaceHolders = require('./_replaceHolders');
|
5 |
|
|
|
6 |
|
|
/** Used to compose bitmasks for function metadata. */
|
7 |
|
|
var WRAP_BIND_FLAG = 1,
|
8 |
|
|
WRAP_PARTIAL_FLAG = 32;
|
9 |
|
|
|
10 |
|
|
/**
|
11 |
|
|
* Creates a function that invokes `func` with the `this` binding of `thisArg`
|
12 |
|
|
* and `partials` prepended to the arguments it receives.
|
13 |
|
|
*
|
14 |
|
|
* The `_.bind.placeholder` value, which defaults to `_` in monolithic builds,
|
15 |
|
|
* may be used as a placeholder for partially applied arguments.
|
16 |
|
|
*
|
17 |
|
|
* **Note:** Unlike native `Function#bind`, this method doesn't set the "length"
|
18 |
|
|
* property of bound functions.
|
19 |
|
|
*
|
20 |
|
|
* @static
|
21 |
|
|
* @memberOf _
|
22 |
|
|
* @since 0.1.0
|
23 |
|
|
* @category Function
|
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.
|
28 |
|
|
* @example
|
29 |
|
|
*
|
30 |
|
|
* function greet(greeting, punctuation) {
|
31 |
|
|
* return greeting + ' ' + this.user + punctuation;
|
32 |
|
|
* }
|
33 |
|
|
*
|
34 |
|
|
* var object = { 'user': 'fred' };
|
35 |
|
|
*
|
36 |
|
|
* var bound = _.bind(greet, object, 'hi');
|
37 |
|
|
* bound('!');
|
38 |
|
|
* // => 'hi fred!'
|
39 |
|
|
*
|
40 |
|
|
* // Bound with placeholders.
|
41 |
|
|
* var bound = _.bind(greet, object, _, '!');
|
42 |
|
|
* bound('hi');
|
43 |
|
|
* // => 'hi fred!'
|
44 |
|
|
*/
|
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;
|
50 |
|
|
}
|
51 |
|
|
return createWrap(func, bitmask, thisArg, partials, holders);
|
52 |
|
|
});
|
53 |
|
|
|
54 |
|
|
// Assign default placeholders.
|
55 |
|
|
bind.placeholder = {};
|
56 |
|
|
|
57 |
|
|
module.exports = bind;
|