Last active
August 25, 2016 08:33
-
-
Save donpark/e071d8c0ea96ca3e476c28838af2b5ba to your computer and use it in GitHub Desktop.
bind all onXXX methods of an object
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
export function bindMethods(obj, filter) { | |
if (obj) { | |
for (let name of Object.getOwnPropertyNames(Object.getPrototypeOf(obj))) { | |
let value = obj[name] | |
if (typeof value === 'function' && filter(name, value)) { | |
obj[name] = value.bind(obj) | |
} | |
} | |
} | |
} | |
const eventListenerFilter = (name, method) => /on[A-Z]\w*/.test(name) | |
export function bindEventListeners(obj) { | |
bindMethods(obj, eventListenerFilter) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment