You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.1 KiB
42 lines
1.1 KiB
3 weeks ago
|
import arrayEach from './_arrayEach.js';
|
||
|
import baseAssignValue from './_baseAssignValue.js';
|
||
|
import bind from './bind.js';
|
||
|
import flatRest from './_flatRest.js';
|
||
|
import toKey from './_toKey.js';
|
||
|
|
||
|
/**
|
||
|
* Binds methods of an object to the object itself, overwriting the existing
|
||
|
* method.
|
||
|
*
|
||
|
* **Note:** This method doesn't set the "length" property of bound functions.
|
||
|
*
|
||
|
* @static
|
||
|
* @since 0.1.0
|
||
|
* @memberOf _
|
||
|
* @category Util
|
||
|
* @param {Object} object The object to bind and assign the bound methods to.
|
||
|
* @param {...(string|string[])} methodNames The object method names to bind.
|
||
|
* @returns {Object} Returns `object`.
|
||
|
* @example
|
||
|
*
|
||
|
* var view = {
|
||
|
* 'label': 'docs',
|
||
|
* 'click': function() {
|
||
|
* console.log('clicked ' + this.label);
|
||
|
* }
|
||
|
* };
|
||
|
*
|
||
|
* _.bindAll(view, ['click']);
|
||
|
* jQuery(element).on('click', view.click);
|
||
|
* // => Logs 'clicked docs' when clicked.
|
||
|
*/
|
||
|
var bindAll = flatRest(function(object, methodNames) {
|
||
|
arrayEach(methodNames, function(key) {
|
||
|
key = toKey(key);
|
||
|
baseAssignValue(object, key, bind(object[key], object));
|
||
|
});
|
||
|
return object;
|
||
|
});
|
||
|
|
||
|
export default bindAll;
|