News for hackers. Stuff that matters.
Re:‘This’ in JavaScript by amelius on Thursday August 03, 2017 @05:16AM (#14918083)
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).

Re:‘This’ in JavaScript by carussell on Thursday August 03, 2017 @09:21AM (#14919269)
> 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-...

Re:‘This’ in JavaScript by amelius on Thursday August 03, 2017 @02:36PM (#14922392)
Well 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.
Re:‘This’ in JavaScript by carussell on Saturday August 05, 2017 @01:01PM (#14936631)
> 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.

Re:‘This’ in JavaScript by kitd on Thursday August 03, 2017 @06:23AM (#14918352)
Agreed. 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.

Re:‘This’ in JavaScript by scruple on Thursday August 03, 2017 @12:39PM (#14921112)
This 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.
Re:‘This’ in JavaScript by nkozyra on Thursday August 03, 2017 @07:51AM (#14918724)
I thought self had gone out of favor due to its relatively new global purpose.
Re:‘This’ in JavaScript by kitd on Thursday August 03, 2017 @08:40AM (#14918994)
ok, call it something else. I meant to say, save away the current ref to 'this'.