Re:How JavaScript Works: deep dive into call, apply, and bind
by dimmke on Friday July 09, 2021 @10:35PM
(#27790679)
Seriously. It feels like an entire area of JavaScript I pretty much never wade into anymore.
Re:How JavaScript Works: deep dive into call, apply, and bind
by SilurianWenlock on Saturday July 10, 2021 @04:19AM
(#27791962)
Why not?
Re:How JavaScript Works: deep dive into call, apply, and bind
by cunthorpe on Saturday July 10, 2021 @08:05AM
(#27792800)
Untechnically speaking: Arrow functions preserve the `this` in their declaration point, whereas regular functions change `this` if they're called as methods.
class Foo {
thisCouldBeAnything() {return this}
thisIsDefinitelyFoo = () => this
}
new Foo().thisCouldBeAnything.call('use me')
// 'use me'
new Foo().thisIsDefinitelyFoo.call('unused')
// Foo
Since the intention is often to keep `this` to refer to the class, people have been binding every single method in the class constructor, but this is no longer necessary thanks to arrow functions.
Re:How JavaScript Works: deep dive into call, apply, and bind
by schwartzworld on Saturday July 10, 2021 @05:46AM
(#27792301)
New features were added to the language that make this sort of stuff unecessary.
Re:How JavaScript Works: deep dive into call, apply, and bind
by cmpolis on Saturday July 10, 2021 @01:05AM
(#27791252)
This sparked my memory of using "var self = this;" and "var _this = this;" Can't put my finger on it, but _this_ seems quite funny looking back on it (not to say it didn't work...)
Re:How JavaScript Works: deep dive into call, apply, and bind
by quickthrower2 on Saturday July 10, 2021 @01:11AM
(#27791272)
I use this… but in classes in typescript and I’m quite happy to do so.
Re:How JavaScript Works: deep dive into call, apply, and bind
by Waterluvian on Saturday July 10, 2021 @08:59PM
(#27797672)
I’ve tried to use classes in TS and they’re just so weak that I always regret it.
I tried to have a base class with a default serializer method and bam, right back to binding problems.
Can’t remember the last time I saw a ‘this’. Functional programming forever!