Menu

gitpiper

bindAll javascript Code Snippet in 2024

objectfunctionintermediate

Last Updated: 1 December 2024

Binds methods of an object to the object itself, overwriting the existing method.

  • Use Array.prototype.forEach() to iterate over the given fns.
  • Return a function for each one, using 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.

javascript snippet similar to bindAll For You in December 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️