JS Classes and This
I'm curious if there is a way to avoid using arrow functions for methods you want to keep bound to the instance. For example,
63 Replies
the annoying way is
I know I can do
const notworks = pog.notworks.bind(pog)
but thats way worse than an arrow fnwhat do you have against using arrow functions?
I think they're less readable
But thanks, works2 is what I was looking for i guess
I don't think many people would agree with that sentiment if the alternative to arrow functions is manually binding the this keyword IMO
Yeah it feels icky
I mean it's probably good to learn some of the outdated JS stuff so that way you get to know the gotchas in case you're in a codebase with it.
do people just...always use arrow functions in classes then?
pretty much I think
fair enough
I also think (I could be wrong), in typescript it does that for you
So I'll see the ES5 syntax in typescript more often
no i was running into an issue with this in ts
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
interesting
I know notworks works in that example, maybe it's a version thing?
Well, that works but the example would actually be calling
Not sure if it's a TS thing tho, the difference is that notworks() assigns the method to the prototype and works = () => assigns the method to the instance
I've literally gone years without knowing how
this
works in js lol