Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
So, is there any other options to avoid using this, that are native to JS? I know one of the quickest ways is var self = this, but not about anything that do not involves a variable. And one of your past comments, wake up my curiosity, if TypeScript is a superset of JS, what it does to work aroun with this issues?
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Although I can see what you are pointing, what is difficult, is to find the best way to avoid using this in a real application. Thanks a lot, this has been one of the most significant discussion this year.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
That is because you have been taught to use this. With OOP, you are pretty much stuck with this (unless of course you use nothis). When you learn functional reactive programming, you'll find this to no longer be necessary and magically this just vanishes from your codebase.
If you use typescript and modify the cat speak code to use an explicit if, you get a compile error.
constcat={sound:'meow',speak(this:{sound:string}){returnthis.sound}}constspeak=cat.speakspeak()// <= Compile error: void is not assignable to { sound: string }.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
It is actually. it's just that your scope can change when you do not expect it to change.
So, is there any other options to avoid using this, that are native to JS? I know one of the quickest ways is
var self = this
, but not about anything that do not involves a variable. And one of your past comments, wake up my curiosity, if TypeScript is a superset of JS, what it does to work aroun withthis
issues?TypeScript doesn't fix all
this
issues.If you check out the REPL at typescriptlang.org/play/ and enter this code:
You'll find it still has problems with
this
.My solution to avoiding this is to write JavaScript functionally and not to mix data and functions together into classes.
I would instead write the above like this:
Although I can see what you are pointing, what is difficult, is to find the best way to avoid using
this
in a real application. Thanks a lot, this has been one of the most significant discussion this year.That is because you have been taught to use
this
. With OOP, you are pretty much stuck withthis
(unless of course you usenothis
). When you learn functional reactive programming, you'll findthis
to no longer be necessary and magicallythis
just vanishes from your codebase.Check out one of my projects here github.com/joelnet/bitcoin-all-tim.... This is a real world application. It follows what I preach from How I rediscovered my love for JavaScript after throwing 90% of it in the trash.
You won't find any references to
this
,var
,let
,if
,switch
,for
, thefunction
keyword or other staples of classic javascript programs.If you use typescript and modify the cat speak code to use an explicit if, you get a compile error.
It's pretty nice that this will happen at compile time and not runtime.