4 ms·
The problem is that the approach is different depending on context. You are making use of the assumption that the addEventListener function knows to call the "h
by amelius 9y ago
The problem is that the approach is different depending on context. You are making use of the assumption that the addEventListener function knows to call the "handleEvent" method of the object in the second argument, and give it a proper "this" object. That's a big assumption, which should be documented in the code.
The code using "self" doesn't have this problem; it's more self-contained so to speak (no pun intended).
- kitd 9y agoAgreed. Much prefer 'var self = this' style. Keep the model transparent for future maintenance. 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.
- nkozyra 9y agoI thought self had gone out of favor due to its relatively new global purpose.
- kitd 9y agook, call it something else. I meant to say, save away the current ref to 'this'.
- scruple 9y agoThis is where I've landed with the JavaScript that I write. It keeps things relatively sane, at least better than the other approaches I've tried.
- carussell 9y ago> That's a big assumption 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-EventListener https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-...
- amelius 9y agoWell I don't see it used in a lot of code, so in that respect I think it is obscure. Also, using the "self" approach allows one to lexically bind to the enclosing scope, which aligns nicely with how most functional programmers think.
- carussell 9y ago> Well I don't see it used in a lot of code, so in that respect I think it is obscure. You just moved the goalposts.