Created
April 5, 2017 05:16
-
-
Save ronen-e/8f0740a87bf5ddf689d39617b3aa6eec to your computer and use it in GitHub Desktop.
autobind implementation - bind all prototype methods to instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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