Array.prototype.forEach()
to iterate over the given fns
.Function.prototype.apply()
to apply the given context (obj
) to fn
.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.
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️