Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseFlatten = require('../internal/baseFlatten'),
2
createWrapper = require('../internal/createWrapper'),
3
functions = require('../object/functions'),
4
restParam = require('./restParam');
5
6
/** Used to compose bitmasks for wrapper metadata. */
7
var BIND_FLAG = 1;
8
9
/**
10
* Binds methods of an object to the object itself, overwriting the existing
11
* method. Method names may be specified as individual arguments or as arrays
12
* of method names. If no method names are provided all enumerable function
13
* properties, own and inherited, of `object` are bound.
14
*
15
* **Note:** This method does not set the "length" property of bound functions.
16
*
17
* @static
18
* @memberOf _
19
* @category Function
20
* @param {Object} object The object to bind and assign the bound methods to.
21
* @param {...(string|string[])} [methodNames] The object method names to bind,
22
* specified as individual method names or arrays of method names.
23
* @returns {Object} Returns `object`.
24
* @example
25
*
26
* var view = {
27
* 'label': 'docs',
28
* 'onClick': function() {
29
* console.log('clicked ' + this.label);
30
* }
31
* };
32
*
33
* _.bindAll(view);
34
* jQuery('#docs').on('click', view.onClick);
35
* // => logs 'clicked docs' when the element is clicked
36
*/
37
var bindAll = restParam(function(object, methodNames) {
38
methodNames = methodNames.length ? baseFlatten(methodNames) : functions(object);
39
40
var index = -1,
41
length = methodNames.length;
42
43
while (++index < length) {
44
var key = methodNames[index];
45
object[key] = createWrapper(object[key], BIND_FLAG, object);
46
}
47
return object;
48
});
49
50
module.exports = bindAll;
51
52