861 B
861 B
title, tags, expertise, cover, firstSeen, lastUpdated
| title | tags | expertise | cover | firstSeen | lastUpdated |
|---|---|---|---|---|---|
| Bind all object methods | object,function | intermediate | blog_images/laptop-with-code.jpg | 2018-01-26T14:14:53+02:00 | 2020-11-03T22:11:18+02:00 |
Binds methods of an object to the object itself, overwriting the existing method.
- Use
Array.prototype.forEach()to iterate over the givenfns. - Return a function for each one, using
Function.prototype.apply()to apply the given context (obj) tofn.
const bindAll = (obj, ...fns) =>
fns.forEach(
fn => (
(f = obj[fn]),
(obj[fn] = function() {
return f.apply(obj);
})
)
);
var view = {
label: 'docs',
click: function() {
console.log('clicked ' + this.label);
}
};
bindAll(view, 'click');
document.body.addEventListener('click', view.click);
// Log 'clicked docs' when clicked.