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 ahandleEventfunction 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 😳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
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:
callmoves the hiddenthisto aselfparameter (ie: rust).applymoves the parameters tuple list to a tuple or fixed array.bindmoves the hiddenthisto aselfand creates a context using it.thisis the object it was called upon; or undefined in toplevel strict mode.thiscontext 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
thishidden parameter into theselfleading parameter. I use this a lot actually... my typescript for it isFurthermore, including
ReflectandProxy:Reflect.applyadds a dynamic dispatcher (function) option.Reflect.constructadds 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...really amazing article!!
Thank you for dedicating time on THIS.
it really helped a lot!
Thank you Tania! I needed a refresher:-)
Awesome post...loved it
This (no pun intended...) article was an amazing read. Very very helpful.
Great article
Great article!
I have been struggling with this for a very long time.
This was a very good explanation.
Best article/post on "this"..... !
Excellent article
A concise article, thanks!
God herself wrote this article. This is really a piece of art! :-)