Skip to content

Instantly share code, notes, and snippets.

@ronen-e
Created April 5, 2017 05:16
Show Gist options
  • Save ronen-e/8f0740a87bf5ddf689d39617b3aa6eec to your computer and use it in GitHub Desktop.
Save ronen-e/8f0740a87bf5ddf689d39617b3aa6eec to your computer and use it in GitHub Desktop.
autobind implementation - bind all prototype methods to instance
function binder(instance) {
for (var prop in instance) {
if (typeof instance[prop] === 'function' && !instance.hasOwnProperty(prop)) {
instance[prop] = instance[prop].bind(instance);
}
}
}
function binder2(instance, prototype) {
Object.keys(prototype)
.filter(prop => typeof prototype[prop] === 'function')
.forEach(prop => instance[prop] = prototype[prop]);
}
// binder(a);
// binder2(a, A.prototype);
// console.log(Object.keys(a));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment