1 | import arrayEach from './_arrayEach';
|
2 | import baseFlatten from './_baseFlatten';
|
3 | import bind from './bind';
|
4 | import rest from './rest';
|
5 |
|
6 | /**
|
7 | * Binds methods of an object to the object itself, overwriting the existing
|
8 | * method.
|
9 | *
|
10 | * **Note:** This method doesn't set the "length" property of bound functions.
|
11 | *
|
12 | * @static
|
13 | * @since 0.1.0
|
14 | * @memberOf _
|
15 | * @category Util
|
16 | * @param {Object} object The object to bind and assign the bound methods to.
|
17 | * @param {...(string|string[])} methodNames The object method names to bind,
|
18 | * specified individually or in arrays.
|
19 | * @returns {Object} Returns `object`.
|
20 | * @example
|
21 | *
|
22 | * var view = {
|
23 | * 'label': 'docs',
|
24 | * 'onClick': function() {
|
25 | * console.log('clicked ' + this.label);
|
26 | * }
|
27 | * };
|
28 | *
|
29 | * _.bindAll(view, 'onClick');
|
30 | * jQuery(element).on('click', view.onClick);
|
31 | * // => Logs 'clicked docs' when clicked.
|
32 | */
|
33 | var bindAll = rest(function(object, methodNames) {
|
34 | arrayEach(baseFlatten(methodNames, 1), function(key) {
|
35 | object[key] = bind(object[key], object);
|
36 | });
|
37 | return object;
|
38 | });
|
39 |
|
40 | export default bindAll;
|