PSA Don't do this:
proto.foo = function() {
/* ... */
var self = this;
element.addEventListener("click", (function(event) {
self.iHaveAnUnhealthyObsessionWithLambdas(event.target);
}));
}
proto.iHaveAnUnhealthyObsessionWithLamdas = function(clicked) {
/* do something with clicked element `clicked` */
}
Do this instead: proto.foo = function() {
/* ... */
element.addEventListener("click", this);
}
proto.handleEvent = function(event) {
/* do something with clicked element `event.target` */
} class ElementDragger {
constructor(elm) {
elm.addEventListener('mousedown', this);
}
handleEvent(e) {
const elm = e.target;
switch(e.type) {
case 'mousedown':
elm.removeEventListener('mousedown', this).
elm.addEventListener('mousemove', this);
elm.addEventListener('mouseup', this);
break;
case 'mousemove':
elm.style.left = e.clientX + 'px';
break;
case 'mouseup':
elm.removeEventListener('mousedown', this);
elm.removeEventListener('mouseup', this);
elm.addEventListener('mousedown', this);
break;
}
}
}I don't know the purpose unbinding mousedown on mousedown - it only fires once per mousedown doesn't it?
Why not just stick with `elm.addEventListener('mousedown', this.handleMouseDown.bind(this))`? Where's the benefit other than making things more verbose?
> Where's the benefit other than making things more verbose?
To be clear what you're asking, are you saying that:
elm.addEventListener('mousedown', this);
... is more verbose than: elm.addEventListener('mousedown', this.handleMouseDown.bind(this))Switch statements in OOP are a sign that something is wrong.
handleEvent(e) {
this[e.type](e, e.target);
}
mousemove(e, elm) {
elm.style.left = e.clientX + 'px';
}
... but then the discussion would almost certainly revolve around this questionable decision instead of the original topic.Then again, my jugment might be colored by working on codebases that are >21 years old.
You're vacillating and applying an inconsistent standard. The switch statement is used where multiple listeners are being attached, therefore it wouldn't work with your "single function". You'd need multiple functions.
Give any example passing functions directly as the callback to `addEventListener` to demonstrate your case, please. The equivalent version that simply implements DOMEventListener will be less verbose, be lighter on resources (both with memory use and with runtime), and have no need for any this-binding workaround weirdness.
this._callback = this.handleMouseDown.bind(this);
elm.addEventListener('mousedown', this._callback);
elm.removeEventListener('mousedown', this._callback);
this._callback = null;
Compared to: elm.addEventListener('mousedown', this);
elm.removeEventListener('mousedown', this);
@richthegeek I removed that event listener in my example to illustrate how easy it is, there was otherwise no good reason for it.In general, a good practice is to avoid anonymous functions for the listener else it is not possible to remove the event later.
['click', 'focus', 'blur'].forEach(type => elm.removeEventListener(type, this));
The code using "self" doesn't have this problem; it's more self-contained so to speak (no pun intended).
One time I don't is in Jasmine, where they explicitly state that 'this' is shared across all 'beforeEach', 'it' and 'afterEach' calls. Which backs up your first point.
No, it's not. At all. It's part of the DOM Level 2 standard that specifies what `addEventListener` does and has been for going on 20 years.
https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-...
You just moved the goalposts.
We don't like it and it's redundant. One should learn the principles and return to this article to read it as a quiz and find out whether he got it or not.
Books like the excellent Javascript Allonge (free to read on leanpub) do a great job on explaining the context. If you need more hardcore stuff, "You don't know JS" will fit you better.
(Disclaimer: I'm not affiliated with any of the recommended books or their authors or leanpub or whatever.They just helped me a lot; and still do from time to time)
Exactly.
"If you can't explain it simply, you don't understand it well enough."
Albert Einstein