addEventListener basic example
proposal for http://developer.mozilla.org/en/DOM/element.addEventListener
by apimenov93
HTML
<div id="ear">Click me</div>
<div id="display"></div>
JavaScript
'use strict';
class myClass {
myMethod(args) {
console.log('you got them superior coding skillz');
}
myCallback = function(e) {
console.log("hi");
this.myMethod(null);
var key = e.keyCode ? e.keyCode : e.which;
if(key === 27) {
this.myMethod(args);
}
};
listeners(addListener) {
if(addListener === true) {
document.getElementById('ear').addEventListener('click', this.myCallback, false);
}
else {
console.log("removed"+this.myCallback);
document.getElementById('ear').removeEventListener('click', this, false);
}
}
}
MyClass.prototype = {
// Implement the interface
handleEvent: function(e) {
// `this` is your object
// verify that there's a handler for the event type, and invoke it
return this[e.type] && this[e.type](e);
},
click: function (e) {
this.myCallback(null);
// `this` is your object
}
}
var cl = new myClass();
cl.listeners(true);