Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var createWrapper = require('../internal/createWrapper'),
2
replaceHolders = require('../internal/replaceHolders'),
3
restParam = require('./restParam');
4
5
/** Used to compose bitmasks for wrapper metadata. */
6
var BIND_FLAG = 1,
7
PARTIAL_FLAG = 32;
8
9
/**
10
* Creates a function that invokes `func` with the `this` binding of `thisArg`
11
* and prepends any additional `_.bind` arguments to those provided to the
12
* bound function.
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 does not set the "length"
18
* property of bound functions.
19
*
20
* @static
21
* @memberOf _
22
* @category Function
23
* @param {Function} func The function to bind.
24
* @param {*} thisArg The `this` binding of `func`.
25
* @param {...*} [partials] The arguments to be partially applied.
26
* @returns {Function} Returns the new bound function.
27
* @example
28
*
29
* var greet = function(greeting, punctuation) {
30
* return greeting + ' ' + this.user + punctuation;
31
* };
32
*
33
* var object = { 'user': 'fred' };
34
*
35
* var bound = _.bind(greet, object, 'hi');
36
* bound('!');
37
* // => 'hi fred!'
38
*
39
* // using placeholders
40
* var bound = _.bind(greet, object, _, '!');
41
* bound('hi');
42
* // => 'hi fred!'
43
*/
44
var bind = restParam(function(func, thisArg, partials) {
45
var bitmask = BIND_FLAG;
46
if (partials.length) {
47
var holders = replaceHolders(partials, bind.placeholder);
48
bitmask |= PARTIAL_FLAG;
49
}
50
return createWrapper(func, bitmask, thisArg, partials, holders);
51
});
52
53
// Assign default placeholders.
54
bind.placeholder = {};
55
56
module.exports = bind;
57
58