I'm trying to create a custom module. I'm using prototype
to add additional methods. The module will have an event, in which I will have to access a method from the module with 'this
'. I will do that using bind
.
I ran into a slight problem. I created a method, then using prototype
, I assigned the method to a variable
with the bind
method. The problem is, 'this
' isn't the one I passed through bind
. Here's the sample code:
JSFiddle
function MyPlugin() {
this.hello = 'hello'
document.addEventListener('click', this.clickedBinded);
this.clickedBinded('e');
}
MyPlugin.prototype.clickedBinded = clicked.bind(this);
function clicked(e) {
console.log(this.hello, e);
}
var plugin1 = new MyPlugin();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…