29 lines
627 B
Markdown
29 lines
627 B
Markdown
### bindAll
|
|
|
|
Binds methods of an object to the object itself, overwriting the existing method.
|
|
|
|
Use `Array.forEach()` to return a `function` that uses `Function.apply()` to apply the given context (`obj`) to `fn` for each function specified.
|
|
|
|
```js
|
|
const bindAll = (obj, ...fns) =>
|
|
fns.forEach(
|
|
fn => (
|
|
(f = obj[fn]),
|
|
(obj[fn] = function() {
|
|
return f.apply(obj);
|
|
})
|
|
)
|
|
);
|
|
```
|
|
|
|
```js
|
|
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.
|
|
```
|