I'm actually getting stuck on a simple (pretty sure it is) example.
<div id="first">first</div>
<div id="second">second</div>
<script>
var JSClass = {
element : null,
click : function() {
console.log(this.element.id);
},
created : function(element) {
this.element = element;
this.element.addEventListener('click', this.click.bind(this));
}
};
JSClass.created(document.querySelector('#first'));
JSClass.created(document.querySelector('#second'));
</script>
By binding the entire object when I create the event I should obtain "first" when I click on the first element and "second" when I click on the second element but it's not the case. It's fully functional for other objects I use but this simple case doesn't work...
.created()the second time, it overwrites the value of the "element" property that was set when you called it the first time.JSClassto a function which returns the object.