5 ms·
> window.addEventListener('online', () => this.checkStorage()); Every time I see this double function safety pattern a small part of my JS heart dies.
by noiv 9y ago
> window.addEventListener('online', () => this.checkStorage());
Every time I see this double function safety pattern a small part of my JS heart dies.
- allover 9y agoEh, you prefer using bind() or something? Because that still creates another function.
- deleted 9y ago[deleted]
- wongarsu 9y agoWhat's the better alternative?
- reificator 9y agoIf I understand their concerns correctly, they would prefer this: > window.addEventListener('online', this.checkStorage);
- allover 9y agoThat wouldn't work, you'd need to at least use bind() so that the `this` keyword within `this.checkStorage` references the right thing, e.g. window.addEventListener('online', this.checkStorage.bind(this));
- mikewhy 9y agoThe language can do that for you: class Foo { checkStorage = () => { // Do check storage stuff here } } And the language will take care of binding the function. (Really, it's just doing `this.checkStorage = this.checkStorage.bind(this)` in the constructor).
- allover 9y agoThat's not in 'the language' yet :)
- deleted 9y ago[deleted]
- TomMarius 9y agoUsing bind and saving a reference to the bound function one level up, not creating a new one each time
- deleted 9y ago[deleted]