The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.
The this keyword is a ver...
For further actions, you may consider blocking this person and/or reporting abuse
This is a fantastic post covering the intricacies and nuances of
this
! If you're open to receiving suggestions, I think a section that covers how implementing ahandleEvent
function within an object ensures proper scoping ofthis
(with almost zero overhead to memory and CPU) to the object when it is used in cases likeaddEventListener(event, objectImplementingHandleEvent)
. It's a ridiculously powerful pattern, no joke 😳Great article, thank you!
I wonder if it is worth explicitly pointing out in the "fat arrow" example, that the "arrowFunction" call to "this" could be pointing at global scope "this".
That alone seems like a good argument for avoiding the use of "this" entirely, as it feels like it violates the "Principal of Least Surprise".
(sorry for all the quotes, just seemed to make sense)
May as well do it:
call
moves the hiddenthis
to aself
parameter (ie: rust).apply
moves the parameters tuple list to a tuple or fixed array.bind
moves the hiddenthis
to aself
and creates a context using it.this
is the object it was called upon; or undefined in toplevel strict mode.this
context of their own. In rust the closest is a closure withmove
. This is normal.These can be composed in insane ways:
In this case, I move the
this
hidden parameter into theself
leading parameter. I use this a lot actually... my typescript for it isFurthermore, including
Reflect
andProxy
:Reflect.apply
adds a dynamic dispatcher (function) option.Reflect.construct
adds a class dispatcher (class/new) option.Proxy { apply() {} }
is the inverse ofReflect.apply
; and can be heavily reused in children.Proxy { construct() {} }
Adding getters and setters:
get a() {}
returns some value representinga
; which may be a constant or variable or an internal value. This can also have side effects...set a(val) {}
returns void (undefined) and setsa
; or else throws when there's some issue withval
. For example, checkingNumber.isInteger(val)
and throwing when it fails. Side effects allowed here as well...tl;dr; (opinionated)
Code in es6+ and you will never need call, apply and bind. Fat arrow will do the hard work for you.
But read this article. You have to know why you need fat arrow and when is really needed.
I don't think that's the tl;dr...
sorry, forgot to mention that this is my personal perspective from my experience
really amazing article!!
Thank you for dedicating time on THIS.
it really helped a lot!
Thank you Tania! I needed a refresher:-)
Best article/post on "this"..... !
Excellent article
This (no pun intended...) article was an amazing read. Very very helpful.
A concise article, thanks!
Great article!
I have been struggling with this for a very long time.
This was a very good explanation.
Awesome post...loved it
Great article
God herself wrote this article. This is really a piece of art! :-)