Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
This is absolutely a fantastic solution and it definitely solves one of this's problems. This is a perfect example of that.
I still prefer to eliminate this completely. I don't want to have to ask if the function has been written correctly so that this behaves the way I expect it.
I don't have to worry about "it's okay to use here, this way, but not over here." It's easier for me to program without it.
It's much like null. An even simpler concept than this. We know to put nullchecks all over.
// check that nullif(x){x.y()}// shortcut checkx&&x.y()
But no matter how hard we try, we are still destined to run into NullReferenceException.
And much like Douglas Crockford says...
"I was very surprised to discover that my programs got better. It was not a hardship to not use this. It was actually a benefit. My programs got smaller and easier and you know, that's what we're all looking for" -- Douglas Crockford
... banning this in your programs, much like null won't be a hardship. It will become a benefit, making your programs smaller and easier!
The only way to eliminate 100% of this related bugs, is to eliminate this.
Unfortunately, this code is not idiomatic as you should pass a function to the setState in this case or else from several increments in a row most get ignored as the state is updated asynchronously and they will have a stale state... nothis FTW is what I take feom this comment in the end...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Fat arrow functions make old complaints about "this" moot, especially in React. Instead of
simply do:
Benefits:
#thisIsFine
This is absolutely a fantastic solution and it definitely solves one of
this
's problems. This is a perfect example of that.I still prefer to eliminate
this
completely. I don't want to have to ask if the function has been written correctly so thatthis
behaves the way I expect it.I don't have to worry about "it's okay to use here, this way, but not over here." It's easier for me to program without it.
It's much like
null
. An even simpler concept thanthis
. We know to put nullchecks all over.But no matter how hard we try, we are still destined to run into
NullReferenceException
.And much like Douglas Crockford says...
... banning
this
in your programs, much likenull
won't be a hardship. It will become a benefit, making your programs smaller and easier!The only way to eliminate 100% of
this
related bugs, is to eliminatethis
.Cheers!
Unfortunately, this code is not idiomatic as you should pass a function to the setState in this case or else from several increments in a row most get ignored as the state is updated asynchronously and they will have a stale state... nothis FTW is what I take feom this comment in the end...