UNPKG

1.12 kBJavaScriptView Raw
1import arrayEach from './_arrayEach.js';
2import baseAssignValue from './_baseAssignValue.js';
3import bind from './bind.js';
4import flatRest from './_flatRest.js';
5import toKey from './_toKey.js';
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 */
33var 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
41export default bindAll;