Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Automatically binds ES6 class methods
- // From within constructor() call classAutoBind(this)
- window.classAutoBind = function(context) {
- for (let method of Object.getOwnPropertyNames(Object.getPrototypeOf(context))) {
- if (method !== 'constructor') {
- context[method] = context[method].bind(context)
- }
- }
- }
- // Example:
- class Tacos {
- constructor() {
- this.type = 'Tacos'
- classAutoBind(this)
- }
- getType() {
- return this.type
- }
- }
- var tacos = new Tacos()
- console.log(tacos.getType())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement