Last active
February 11, 2016 05:53
-
-
Save arieh/11af0b5d244ddae25d67 to your computer and use it in GitHub Desktop.
Bound mixin
This file contains 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 Bind(){ | |
var i,fn; | |
/** | |
* holds the generated bound function collection | |
* @property bound | |
* @protected | |
* @type object | |
*/ | |
this.bound = function(name){ | |
if (!this.bound[name]){ | |
try { | |
this.bound[name] = this[name].bind(this); | |
} catch (e) { | |
console.warn("Trying to bind undefined method " + name); | |
return; | |
} | |
} | |
return this.bound[name]; | |
}; | |
if (!this.bind) return; | |
}; | |
//usage | |
class a{ | |
constructor(){ | |
Bind.call(this); | |
document.addEventListener('click', this.bound('onClick')); | |
} | |
onClick(){ | |
console.log('click!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment