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