One of the simplest ways I know to make a function/method more reusable is dependency injection. Instead of relying on `this`, pass the dependency into the function as an argument and call the methods on that parameter instead.
-
Show this thread
-
I haven't practiced this yet, but I imagine this can work pretty well with curried functions, too. If you have a dependency you use a lot, pass it in first, bake it in, and then export that function around your app. You still have both functions to reuse in that case!
1 reply 0 retweets 2 likesShow this thread -
One other thing dependency injection shows you is how impure some of your functions are. You can make your functions easier to reason about by not relying on external state so much.
2 replies 0 retweets 2 likesShow this thread -
Replying to @kyleshevlin
My preferred style of JS embraces this
%. I haven't written `class` or `this` more than a handful of times in the last few years.1 reply 0 retweets 2 likes
To add to the benefits list, linters can't pick up mistakes when using `this`. It doesn't know you misspelled `this.databaes`
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.